Announcement Announcement Module
Collapse
No announcement yet.
Connection closed after transaction commit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Connection closed after transaction commit

    I have a question regarding transactions spanning multiple methods.
    This is my applicationContext.xml:

    Code:
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"> 
    	   <property name="targetDataSource"> 
    	      <ref bean="c3p0DataSource"/>
    	   </property> 
    	</bean> 
    		
    	
    	<!-- c3p0 -->
    	<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
            <property name="driverClass"> 
                <value>oracle.jdbc.driver.OracleDriver</value>
            </property> 
            <property name="jdbcUrl"> 
                <value>jdbc&#58;oracle&#58;thin&#58;@localhost&#58;1521&#58;XXXXX</value> 
            </property>
            <property name="initialPoolSize"> 
                <value>0</value> 
            </property>
            <property name="acquireIncrement"> 
                <value>1</value> 
            </property>
            <property name="maxPoolSize"> 
                <value>5</value> 
            </property>
            <property name="maxStatements"> 
                <value>0</value> 
            </property>
            <property name="minPoolSize"> 
                <value>0</value> 
            </property>
            <property name="maxIdleTime"> 
                <value>600</value> 
            </property>
            <property name="user"> 
                <value>user</value> 
            </property> 
            <property name="password"> 
                <value>pwd</value> 
            </property> 
        </bean> 
    
       <bean id="target" class="test.Test">
    	</bean>
    	
    	<bean id="tManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource">
    			<ref bean="dataSource"/>
    		</property>
    	</bean>
    	
    	<bean id="test" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref bean="tManager"/>
    		</property>
    		<property name="target">
    			<ref bean="target"/>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="method1">PROPAGATION_REQUIRED</prop>
    				<prop key="method2">PROPAGATION_REQUIRED</prop>
    				<prop key="method3">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    and this is the code snippet:
    Code:
    public void test&#40;&#41; &#123;
    	Test test = &#40;Test&#41; springCtx.getBean&#40;"test"&#41;;
    	test.method1&#40;&#41;;
    	test.method2&#40;&#41;;
    	test.method3&#40;&#41;;
    &#125;
    I'm using plain JDBC access with DataSourceUtils.getConnection().

    Every time a method is called (method1, method2, method3) a new connection with the db is created,
    a transaction is started and committed, finally the connection is closed:
    1) create connection
    2) execute method1()
    3) commit
    4) close connection
    5) create connection
    6) execute method2()
    7) commit
    [...method3]
    8) close connection

    Is it possible to have only one connection created instead? I mean:

    1) create connection
    2) execute method1()
    3) commit
    4) execute method2()
    5) commit
    6) execute method3()
    7) commit
    8) close connection

    Thank you for help!

  • #2
    You could try setting "maxPoolSize" to one Seriously, why is it important to you to reuse the same connection between the separate transactions?

    Also, if you use DataSourceUtils.getConnection then you don't need the TransactionAwareDataSourceProxy - it's intened for cases where you have legacy JDBC code using DataSource,getConnection() calls.

    Comment


    • #3
      Thank you for your advice with TransactionAwareDataSourceProxy.
      I want to reuse the connection because I have this situation:

      Code:
      public class SampleClass &#123;
      	private SampleDao dao;
      	
      	public void method1&#40;&#41; &#123;
      		dao.setConnection&#40;DataSourceUtils.getConnection&#40;dataSource&#41;&#41;;
      		// use the dao
      	&#125;
      	
      	public void method2&#40;&#41; &#123;
      		// use the same dao in method1&#40;&#41;
      	&#125;
      &#125;
      The dao is legacy code which I cannot modify so I have to set the connection manually.
      If a connection is closed after each method completion, using the same dao in method2()
      cause an exception because the connection used by the dao is already closed.
      It happens with a single connection or with a connection pooling.

      Now, as a workaround, I reset the connection in method2():

      Code:
      	public void method2&#40;&#41; &#123;
      		dao.setConnection&#40;DataSourceUtils.getConnection&#40;dataSource&#41;&#41;;
      		// use the same dao of method1&#40;&#41;
      	&#125;
      Where I do wrong?

      Thank you in advance for any advice.

      Alberto

      Comment


      • #4
        I don't think you are doing anything wrong. Since your methods are running in separate transactions I think it is reasonable to set a new connection on the legacy DAO for each method.

        You could use the SingleConnectionDataSource to hold a connection, but it's a bit cumbersom and now you would have to call the destroy method when you are done to close the connection. You would also have to create a new instance for each request. I've never tried this with multiple transactions, but I think it would work.

        Code:
                SingleConnectionDataSource scds = new SingleConnectionDataSource&#40;dataSourceUtils.getConnection&#40;dataSource&#41;, true&#41;;
        
              // 
              // 
                dao.setConnection&#40;scds.getConnection&#40;&#41;&#41;;
              // 
              // 
        
               scds.destroy&#40;&#41;;

        Comment


        • #5
          How to connection close in spring bean

          Hi friends,
          I am using spring mvc i need connection close in my application if any idea.

          For Example

          i) <bean id="test" class="com.sample.web.sample">
          <property name="url" value="http://www.sample.com" />
          <property name="sampleservicel">
          <ref bean="refsample" />
          </property>
          </bean>

          <bean id="refsample"
          class="com.com.sample.servicecalss"
          scope="prototype" autowire="byName">
          <property name="xmlbean">
          <bean class="com.sample.service.calss" />
          </property>
          </bean>

          This is my bean configure the if any possible for connection close for when i passing the value (resource sharing).

          ii) another on i am deploye the ware file when i undeployed ware clean manually is possible any clean option in java code

          Comment

          Working...
          X