Results 1 to 3 of 3

Thread: PreparedStatements

  1. #1
    Join Date
    Nov 2004
    Posts
    1

    Default PreparedStatements

    I am new to Spring and I was looking at the v1.1.2 JdbcTemplate source and it looks like PreparedStatements are closed after every execution and then recreated the next time the statement is going to be executed. Is this correct? If so, can someone explain the motivation behind closing the statement after using it once. I would like to reuse the PreparedStatement for the next set of parameters if possible, is there someway to do this in Spring? Thank you for any help you can provide.

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    This is appropriate use of the JDBC API. PreparedStatement pooling is the responsibility of the DataSource--for example, that provided by an application server, or Commons DBCP. Please refer to the documentation with your pool or server for how to configure pooling options.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Jul 2005
    Location
    Stamford,CT
    Posts
    6

    Default Prepared Statement in Spring/ Con.close

    2 part Q: This is extension to the Q: maybe redudant abt Prepared Statement and second one is how to use Prepared Statement in Spring

    i have the following class
    Code:
    public GoGreatYetiJava extends DataFather{
    Connection oraCon = null;
    	PreparedStatement psmt = null;
    	Statement stmt = null;
    	private static final String sql = "INSERT INTO YETI_IN_NEWYORK(JK,LG) VALUES("?,?") ":
    public DataSource getOraDataSource() {
    		return oraDataSource;
    
    // now this dataGot is populated everytime it gets a row from some by database by virtue of extending DataFather
    datagot(map){
    Objec l= map.get("JK");
    Objec n= map.get("LG");
    
    //calling insert
    insert(Objec l,Objec n);
    }  
    
    
    public void insert(Object l, Object n) 
    		 {
    		
    		try {
    
    			
    		oraCon = oraDataSource.getConnection();
    		psmt = oraCon.prepareStatement(sql);
    		
    		psmt.setObject(1,l);
    		psmt.setObject(2,n);
    		psmt.executeUpdate();
    
    		
    
    			
    		} catch (SQLException e) {
    			 
    			e.printStackTrace();
    		}finally{
    			if(psmt != null)
    				try {
    					psmt.close();
    				} catch (SQLException e1) {
    					logger.fatal("Cannot close psmt",e1);
    					e1.printStackTrace();
    				}
    			if(oraCon != null)
    				try {
    					oraCon.close();
    				} catch (SQLException e2) {
    					logger.fatal("Cannot close oraCon",e2);
    					e2.printStackTrace();
    				}
    				logger.info("in insert method INSERTED");	
    		}
    		
    	}
    now each time this dataGot gives me 1 row of map's and each time i call the insert()

    am i doing it right?
    meaning opening the connection, then closing it after psmt.executeUpdate(), do we need to close the conn, i am using DataSource so pool is already available, should i close always or just do it once all the values are inserted, how do i know if there are any values left?

    How do we do it using JdbcTemplate?
    i,e prepared statement
    and i tried reading spring ref docs, what u mean by callback methods?

    here's the xml file for dependency injection
    Code:
    <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
    		<property name="url"><value>jdbc&#58;oracle&#58;thin&#58;@kingkong.mar.bogusbank.com&#58;1530&#58;BIGRATD1</value></property>
    		<property name="username"><value>Poigdev</value></property>
    		<property name="password"><value>notalwayswelcome</value></property>
    	</bean>
    i have a Yeti.xml where i do a DP (setter dependcy injection) of dataSource, this and tat the config's are fine through beanRegistry, so the probelm i'm using half of the technology spring provides, still in the learning curve
    Code:
     bean id="yeti" class="com.rat.cat.xock.GoGreatYetiJava">
    		<property name="oraDataSource"><ref bean="DataSource"/></property>
    pls help, if somebody could show the right code and clarify some concepts

Similar Threads

  1. Replies: 7
    Last Post: Sep 13th, 2005, 01:45 AM
  2. problem persisting with manager
    By scottb022563 in forum Data
    Replies: 0
    Last Post: Jul 18th, 2005, 05:45 AM
  3. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  4. Unclosed Hibernate Connection
    By jvargas in forum Data
    Replies: 4
    Last Post: Mar 28th, 2005, 01:24 PM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •