Недавно мы решили изменить некоторые методы из @Transactional
в @Transactional(propagation = Propagation.REQUIRES_NEW)
Hibernate/Spring - JUnit терпит неудачу с транзакционного (REQUIRES_NEW)
и добавил <tx:annotation-driven proxy-target-class="true"/>
в applicationContext.xml
Все отлично работает при запуске приложения, но наши испытания не удались со следующим исключением:
2016-03-15 20:44:02 [main] DEBUG org.hibernate.SQL -
insert
into
utfylling_versjon
(opprettet, utfylling_id, id)
values
(?, ?, ?)
2016-03-15 20:44:02 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [TIMESTAMP] - [Tue Mar 15 20:44:02 CET 2016]
2016-03-15 20:44:02 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [1216]
2016-03-15 20:44:02 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [1217]
2016-03-15 20:44:02 [main] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 23506, SQLState: 23506
2016-03-15 20:44:02 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Referential integrity constraint violation: "FK_JA5LSNJNODJIEEC22M3HU3YIS: PUBLIC.UTFYLLING_VERSJON FOREIGN KEY(UTFYLLING_ID) REFERENCES PUBLIC.UTFYLLING(ID) (1216)"; SQL statement:
insert into utfylling_versjon (opprettet, utfylling_id, id) values (?, ?, ?) [23506-191]
2016-03-15 20:44:02 [main] INFO o.h.e.j.b.internal.AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements
2016-03-15 20:44:02 [main] INFO o.s.t.c.t.TransactionContext - Rolled back transaction for test context [[email protected] testClass = RisikoServiceTest, testInstance = [email protected], testMethod = [email protected], testException = org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FK_JA5LSNJNODJIEEC22M3HU3YIS: PUBLIC.UTFYLLING_VERSJON FOREIGN KEY(UTFYLLING_ID) REFERENCES PUBLIC.UTFYLLING(ID) (1216)"; SQL statement:
insert into utfylling_versjon (opprettet, utfylling_id, id) values (?, ?, ?) [23506-191]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement, mergedContextConfiguration = [[email protected] testClass = RisikoServiceTest, locations = '{classpath:test-context.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FK_JA5LSNJNODJIEEC22M3HU3YIS: PUBLIC.UTFYLLING_VERSJON FOREIGN KEY(UTFYLLING_ID) REFERENCES PUBLIC.UTFYLLING(ID) (1216)"; SQL statement:
insert into utfylling_versjon (opprettet, utfylling_id, id) values (?, ?, ?) [23506-191]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
и более конкретно
Caused by: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK_JA5LSNJNODJIEEC22M3HU3YIS: PUBLIC.UTFYLLING_VERSJON FOREIGN KEY(UTFYLLING_ID) REFERENCES PUBLIC.UTFYLLING(ID) (1216)"; SQL statement:
insert into utfylling_versjon (opprettet, utfylling_id, id) values (?, ?, ?) [23506-191]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
Почему он терпит неудачу, потому что мы запускаем транзакцию с новыми требованиями?
Если я изменить его обратно @Transactional
то все работает нормально, но мы хотим запустить в новой транзакции
EDIT:
Вот часть кода. Я создаю Utfylling.
Utfylling utfylling = someService.createUtfylling();
//Perform some operations
someService.createUtfyllingVersjon(utfylling);
@Transactional
public Utfylling createUtfylling() {
Utfylling utfylling = new Utfylling()
//some setters
entityManager.persist(utfylling);
return utfylling;
}
Тогда я вызываю создать UtfyllingVersjon
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createUtfyllingVersjon(Utfylling utfylling) {
UtfyllingVersjon utfyllingVersjon = new UtfyllingVersjon(utfylling);
entityManager.persist(utfyllingVersjon);
//some more setters
utfylling.getUtfyllingVersjoner().add(utfyllingVersjon);
entityManager.persist(utfyllingVersjon);
entityManager.merge(utfylling);
}
Utfylling отсоединяется, когда речь заходит внутрь createUtfyllingVersjon, так что я должен использовать слияние. Это работает при запуске кода локально в причале, но при запуске теста JUnit он терпит неудачу.
Вот мой тест-context.xml файл
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven proxy-target-class="true"/>
<context:annotation-config />
<context:component-scan base-package="foo.bar"/>
<bean id="dozerMapper" class="org.dozer.DozerBeanMapper" />
<bean id="h2DataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="META-INF/persistence-test.xml"/>
<property name="packagesToScan" value="foo.bar" />
<property name="dataSource" ref="h2DataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaDialect" ref="jpaDialect"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="H2"/>
<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect"/>
<property name="generateDdl" value="true"/>
<property name="showSql" value="true"/>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="h2DataSource"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
</beans>
Я думаю, что этот вопрос не может быть решен из деталей вы предоставляете, потому что это, вероятно, а не просто изменение поведения распространения, которое вызывает проблему, но также то, что вы делаете в существующей транзакции, что вы делаете во вновь созданном и как они соотносятся друг с другом. –
Но почему это работает в одной и той же базе данных, когда я запускаю ее локально по сравнению с модульным тестом? –
У вас есть аннотация '@ TransactionConfiguration' на вашем классе JUnit со значением атрибута по умолчанию для' defaultRollback = true'? –