I've got a spring application that uses JTA (tried both atomikos and bitronix with the same problem) + JPA (Hibernate), and I'm running in the following problem
Everything works since until I try to use foreign keys (for example a OneToOne relationship, cascade doesn't make a difference).
When i do, then the spring transaction synchronizer forcibly close the entity manager before the transaction is finished, and when the transaction manager tries to commit then i just get:
ERROR: HHH000346: Error during managed flush [Session/EntityManager is closed]
There are no other logs that indicate why spring is closing the entity manager early.
I've found out that if I do a entitymenager.flush(), that solves in the problem in most cases, but not all.
I've got a project with unit tests that replicate the problem: https://github.com/Kloudtek/ktspring (the unit tests are those for standalone/atomikos-artemis-hibernate
Here an extract of what I'm using
@Bean(initMethod = "init", destroyMethod = "shutdownForce")
public UserTransactionServiceImp userTransactionService() {
Properties p = new Properties();
p.setProperty("com.atomikos.icatch.service", "com.atomikos.icatch.standalone.UserTransactionServiceFactory");
p.setProperty("com.atomikos.icatch.default_jta_timeout","30000");
return new UserTransactionServiceImp(p);
}
@Bean(initMethod = "init", destroyMethod = "close")
@DependsOn("userTransactionService")
public UserTransactionManager UserTransactionManager() {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(false);
userTransactionManager.setStartupTransactionService(false);
return userTransactionManager;
}
@Bean
@DependsOn("userTransactionService")
public UserTransactionImp userTransactionImp() throws SystemException {
UserTransactionImp userTransactionImp = new UserTransactionImp();
return userTransactionImp;
}
@Bean
@DependsOn("userTransactionService")
public JtaTransactionManager jtaTransactionManager() {
return new JtaTransactionManager(UserTransactionManager(), UserTransactionManager());
}
@Bean
public JPAParams jpaParams() {
Properties p = new Properties();
p.setProperty("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION");
p.setProperty("hibernate.current_session_context_class", "jta");
p.setProperty("hibernate.transaction.jta.platform", AtomikosPlatform.class.getName());
return new JPAParams(p);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManager() {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
if (config.isJtaDatasource()) {
entityManager.setJtaDataSource(config.getDataSource());
} else {
entityManager.setDataSource(config.getDataSource());
}
Properties p = new Properties();
p.putAll(config.getJpaProperties());
if (jpaParamsList != null) {
for (JPAParams jpaParams : jpaParamsList) {
p.putAll(jpaParams.getProperties());
}
}
entityManager.setJpaProperties(p);
entityManager.setPackagesToScan(config.getPackageToScan());
entityManager.setPersistenceProvider(new HibernatePersistenceProvider());
return entityManager;
}
@Entity
public class TestObj {
@Id
private int id;
@OneToOne
private TestObj2 testObj2;
public TestObj() {
}
public TestObj(int id) {
this.id = id;
}
public TestObj(int id, TestObj2 testObj2) {
this.id = id;
this.testObj2 = testObj2;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@Entity
public class TestObj2 {
@Id
private int id;
public TestObj2() {
}
public TestObj2(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public void testDb() {
tx.execute(status -> {
entityManager.persist(new TestObj3(0));
TestObj2 o2 = new TestObj2(0);
entityManager.persist(o2);
TestObj o1 = new TestObj(0, o2);
entityManager.persist(o1);
// If i flush the problem disapears
// entityManager.flush();
return null;
});
}