Results 1 to 4 of 4

Thread: JpaRepository closes the mandatory transaction

  1. #1
    Join Date
    Jun 2012
    Posts
    12

    Default JpaRepository closes the mandatory transaction

    Hi all, I use the JpaRepository interface extension for getting crud operations done. I want the transaction to be wrapped around the controller metho, so I annotated the controlle with @Transactional(propagation = Propagation.REQUIRES_NEW) and the service and the repository with @Transactional(propagation = Propagation.MANDATORY).
    Now I want that if I throw an exception in the controller method or in the service method the transaction is rolled back, instead now the repository commit the transaction and close the connection, so when I throw my exception the data is already persisted in the database. Here is my configuration:

    Code:
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${db.driver}" />
    		<property name="url" value="${db.url}" />
    		<property name="username" value="${db.username}" />
    		<property name="password" value="${db.password}" />
    	</bean>
    
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="packagesToScan" value="${entity.component-scan.base-package}" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="database" value="${hibernate.target_database}" />
    				<property name="generateDdl" value="${hibernate.generate_ddl}" />
    				<property name="showSql" value="${hibernate.show_sql}" />
    				<property name="databasePlatform" value="${hibernate.dialect}" />
    			</bean>
    		</property>
    	</bean>
    
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    My Controller is annotated this way:

    Code:
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Controller
    public class IndexController extends BaseController {
    ...
    }
    The service is annotated this way:

    Code:
    @Transactional(propagation=Propagation.MANDATORY)
    @Service
    public class SampleServiceImpl implements SampleService{
    ...
    }
    and this is the repository:

    Code:
    @Transactional(propagation=Propagation.MANDATORY)
    @Repository
    public interface SampleRepository extends JpaRepository<SampleModel, Long> {
    	
    	@Query("select sm from SampleModel sm where sm.sampleData1 = :sampleData1 and sm.sampleData2 = :sampleData2")
    	SampleModel findUser(@Param("sampleData1") String sampleData1, @Param("sampleData2") String sampleData2);
    
    }
    Please help me out with this stuff.
    Bye

    Daniel

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

    Default

    Is committing, are you sure... Which database are you using? Make sure that you use tables that support transactions (if you use MySQL with MyISAM tables transactions aren't supported).
    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
    Jun 2012
    Posts
    12

    Default

    Hi Marten, thank you for your reply. Yes, is committing, I'm on mysql 5 with InnoDB tables, tryed with jboss 7 and tomcat 7, here's the log of tomcat:

    Code:
    16:20:36,246 DEBUG SessionImpl:253 - Opened session at timestamp: 13400292362
    16:20:36,249 DEBUG AbstractTransactionImpl:158 - begin
    16:20:36,250 DEBUG LogicalConnectionImpl:295 - Obtaining JDBC connection
    16:20:36,259 DEBUG LogicalConnectionImpl:301 - Obtained JDBC connection
    16:20:36,260 DEBUG JdbcTransaction:69 - initial autocommit status: true
    16:20:36,260 DEBUG JdbcTransaction:71 - disabling autocommit
    16:20:36,278 DEBUG ActionQueue:202 - Executing identity-insert immediately
    16:20:36,307 DEBUG SQL:104 - insert into sample (sampleData1, sampleData2) values (?, ?)
    Hibernate: insert into sample (sampleData1, sampleData2) values (?, ?)
    16:20:36,342 DEBUG IdentifierGeneratorHelper:93 - Natively generated identity: 36
    16:20:36,347 DEBUG AbstractTransactionImpl:173 - committing
    16:20:36,348 DEBUG AbstractFlushingEventListener:143 - Processing flush-time cascades
    16:20:36,349 DEBUG AbstractFlushingEventListener:183 - Dirty checking collections
    16:20:36,352 DEBUG AbstractFlushingEventListener:117 - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
    16:20:36,352 DEBUG AbstractFlushingEventListener:124 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
    16:20:36,354 DEBUG EntityPrinter:114 - Listing entities:
    16:20:36,355 DEBUG EntityPrinter:121 - com.domosafety.appname.models.SampleModel{sampleData2=data2ex, id=36, sampleData1=data1ex}
    16:20:36,358 DEBUG JdbcTransaction:113 - committed JDBC Connection
    16:20:36,359 DEBUG JdbcTransaction:126 - re-enabling autocommit
    16:20:36,361 DEBUG LogicalConnectionImpl:314 - Releasing JDBC connection
    16:20:36,361 DEBUG LogicalConnectionImpl:332 - Released JDBC connection
    16:20:36,362 DEBUG ConnectionProxyHandler:219 - HHH000163: Logical connection releasing its physical connection
    16:20:36,364 DEBUG SampleService:32 - Throwing exception...
    18 juin 2012 16:20:36 org.apache.catalina.core.StandardWrapperValve invoke
    GRAVE: Servlet.service() for servlet [dispatcherServlet] in context with path [/spring-webapp-template] threw exception [Request processing failed; nested exception is java.lang.Exception: Error] with root cause
    java.lang.Exception: Error
    	at com.domosafety.appname.services.impl.SampleServiceImpl.insertSampleDataWithErrors(SampleServiceImpl.java:33)
    ...
    the last exception is the one I throw and from which I expect the rollback, it is in the service method now, same behavior when I use it in the controller method. Once the repository call ended the connection has been already committed and closed.
    Any help is appreciated,
    Thank you

    Daniel

  4. #4
    Join Date
    Jun 2012
    Posts
    12

    Default

    Any idea?

    Bye

    Daniel

Posting Permissions

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