Results 1 to 6 of 6

Thread: Required New transaction, is not creating new transaction.

  1. #1
    Join Date
    Jul 2011
    Posts
    24

    Default Required New transaction, is not creating new transaction.

    Hi,

    In my service class I have two methods, both are Transactional. I want to create a new Transaction when control enters into second method from first method. Because what I am doing in second one is totally different and independent from first and should be transactional. So I put Propagation.Required_new above that method. But it is not working, I mean it comes under first transaction.

    I am using TransactionAwareDataSourceProxy datasource as I have OracleDataSource type of datasource. Spring configuration:-

    Code:
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="proxyDataSource"/>
    	</bean>
    	
    	<bean id="proxyDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
    		<property name="targetDataSource" ref="dataSource" />
    	</bean>
    	
    	<bean id="service" class="com.rest.services.ServiceImpl">
    		<property name="dataSource" ref="proxyDataSource" />
    	</bean>
    I read somewhere that to use Required_new, JTA TransactionManager is required. But I need to deploy my application into tomcat and Websphere.

    Any help..

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    I read somewhere that to use Required_new, JTA TransactionManager is required.
    That is bullocks..


    In my service class I have two methods, both are Transactional. I want to create a new Transaction when control enters into second method from first method.
    What you want isn't possible due to the fact that spring uses proxies to apply aop, I suggest a read of the aop chapter especially the part that explains proxies. If you want a new transaction make sure that that method call passes through a proxy. In short you will have to move that method to another service which you call from that method or do manual transaction mgmt for that method and wrap it in a TransactionTemplate which starts a new transaction.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2011
    Posts
    24

    Default

    Thanks Marten, I worked very well. But there is a small problem, I am sure you would solve it. Actually I am passing connection as the argument to the second method where it executes some DB operation with that connection only.
    So I am expecting this connection to take part in new transaction. I think it is not possible because I am not getting expected result. And in debug log I found that second transaction has different connection.

    Code:
    Returning cached instance of singleton bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
    Adding transactional method 'service' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.sql.SQLException
    Returning cached instance of singleton bean 'transactionManager'
    Creating new transaction with name [com.rest.services.DocumentServiceImpl.service]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.sql.SQLException
    Acquired Connection [oracle.jdbc.driver.LogicalConnection@1194cf5] for JDBC transaction
    Switching JDBC Connection [oracle.jdbc.driver.LogicalConnection@1194cf5] to manual commit
    Bound value [org.springframework.jdbc.datasource.ConnectionHolder@16ac92e] for key [com.OracleDataSource@571615] to thread [http-8080-1]
    Initializing transaction synchronization
    Getting transaction for [com.rest.services.DocumentServiceImpl.service]
    service method starts.
    	
    ---call the method 	singalTransactionOperation
    Adding transactional method 'singalTransactionOperation' with attribute: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT; '',-java.sql.SQLException
    Returning cached instance of singleton bean 'transactionManager'
    Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@16ac92e] for key [com.OracleDataSource@571615] bound to thread [http-8080-1]
    Suspending current transaction, creating new transaction with name [com.rest.services.TransactionalHandlerImpl.singalTransactionOperation]
    Clearing transaction synchronization
    Removed value [org.springframework.jdbc.datasource.ConnectionHolder@16ac92e] for key [com.OracleDataSource@571615] from thread [http-8080-1]
    Acquired Connection [oracle.jdbc.driver.LogicalConnection@108b095] for JDBC transaction
    Switching JDBC Connection [oracle.jdbc.driver.LogicalConnection@108b095] to manual commit
    Bound value [org.springframework.jdbc.datasource.ConnectionHolder@1813c12] for key [com.OracleDataSource@571615] to thread [http-8080-1]
    Initializing transaction synchronization
    Getting transaction for [com.rest.services.TransactionalHandlerImpl.singalTransactionOperation]
    singalTransactionOperation method starts.
    
    ----did some insertion
    ----did something that throw SQLException
    Completing transaction for [com.rest.services.TransactionalHandlerImpl.singalTransactionOperation] after exception: java.sql.SQLException: com.dao.DAOException [error=942]: ORA-00942: table or view does not exist
    
    Applying rules to determine whether transaction should rollback on java.sql.SQLException: com.dao.DAOException [error=942]: ORA-00942: table or view does not exist
    Winning rollback rule is: RollbackRuleAttribute with pattern [java.sql.SQLException]
    Triggering beforeCompletion synchronization
    Initiating transaction rollback
    Rolling back JDBC transaction on Connection [oracle.jdbc.driver.LogicalConnection@108b095]
    Triggering afterCompletion synchronization
    Clearing transaction synchronization
    Removed value [org.springframework.jdbc.datasource.ConnectionHolder@1813c12] for key [com.OracleDataSource@571615] from thread [http-8080-1]
    Releasing JDBC Connection [oracle.jdbc.driver.LogicalConnection@108b095] after transaction
    Returning JDBC Connection to DataSource
    Resuming suspended transaction after completion of inner transaction
    Bound value [org.springframework.jdbc.datasource.ConnectionHolder@16ac92e] for key [com.OracleDataSource@571615] to thread [http-8080-1]
    Initializing transaction synchronization
    service method ends.
    Completing transaction for [com.rest.services.DocumentServiceImpl.service]
    Triggering beforeCommit synchronization
    Triggering beforeCompletion synchronization
    Initiating transaction commit
    Committing JDBC transaction on Connection [oracle.jdbc.driver.LogicalConnection@1194cf5]
    Triggering afterCommit synchronization
    Triggering afterCompletion synchronization
    Clearing transaction synchronization
    Removed value [org.springframework.jdbc.datasource.ConnectionHolder@16ac92e] for key [com.OracleDataSource@571615] from thread [http-8080-1]
    Releasing JDBC Connection [oracle.jdbc.driver.LogicalConnection@1194cf5] after transaction
    Returning JDBC Connection to DataSource
    So how would I get that connection. If I get it from the Datasource then would it be same or different. I mean the connection pooling, how would it be handled.

    Thank you very much for assistance.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    You cannot have multiple transactions on the same connection, that isn't possible. It needs a fresh connection to execute on. So yes the other/new transaction operates on a different connection.

    However I don't quite get your problem why would you want that connection? Simply don't pass in a connection and retrieve the connection from the datasource (the proxy) in the new transactional method, that will (at least it should) give you the connection belonging to the current transaction.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jul 2011
    Posts
    24

    Default

    The problem with new connection is that, second method going to called many times like thousands time. So I am afraid that my connection pool is not that much. I know it will release the connection after every transaction but I think there is chance I might not get connection when I call that method with that many time.

    Dose nested transaction uses the same connection for child transaction?

    Thanks

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    In my service class I have two methods, both are Transactional. I want to create a new Transaction when control enters into second method from first method.
    As I stated no it doesn't because you cannot start multiple transactions on the same connection so it really needs a new connection. Which with proper configuration of your connection pool shouldn't be a problem..
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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