-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Strange Behavior at Transaction Management after upgrade
PostPosted: Wed Oct 18, 2017 5:32 am 
Newbie

Joined: Wed Oct 18, 2017 5:13 am
Posts: 3
We face a strange behavior after upgrade from hibernate 4.1.10 to 5.2.10.
The following code is working on version (4.1.10)

Session session1 = sessionFactory.openSession();
System.out.println("session 1 opened");
session1.beginTransaction();
System.out.println("Transaction 1 begin");

/////

Session session2 = sessionFactory.openSession();
System.out.println("session 2 opened");
session2.beginTransaction();
System.out.println("Transaction 2 begin");
session2.getTransaction().commit();
System.out.println("Transaction 2 commit");
session2.close();
System.out.println("session 2 close");

//////

session1.getTransaction().commit();
System.out.println("Transaction 1 commit");
session1.close();
System.out.println("session 1 close");

with the following config

<property name="connection.datasource">.....</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>


and we get the session factory using this code

Configuration configuration = new Configuration();
configuration.configure("com/code/dal/hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);


The same code throw " java.lang.IllegalStateException: Transaction already active" after the upgrade.

The new config is

<property name="connection.datasource">........</property>
<property name="hibernate.transaction.coordinator_class">org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl</property>
<property name="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</property>

and we now get the session factory using this code

StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("com/code/dal/hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();


Top
 Profile  
 
 Post subject: Re: Strange Behavior at Transaction Management after upgrade
PostPosted: Wed Oct 18, 2017 10:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
It's not legal to coordinate transactions manually in JTA. I created a test for this use case:

Code:
try(Session session1 = entityManagerFactory.unwrap(SessionFactory.class).openSession()) {
   session1.beginTransaction();

   try(Session session2 = entityManagerFactory.unwrap(SessionFactory.class).openSession()) {
      session2.beginTransaction();
      session2.getTransaction().commit();

   }
   session1.getTransaction().commit();
}


When I try to run it, I get:

Quote:
java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()

at org.hibernate.internal.AbstractSharedSessionContract.getTransaction(AbstractSharedSessionContract.java:382)
at org.hibernate.internal.AbstractSharedSessionContract.beginTransaction(AbstractSharedSessionContract.java:408)
at com.vladmihalcea.book.hpjp.hibernate.connection.jta.JtaMultipleTransactionsTest.test(JtaMultipleTransactionsTest.java:35)


which is fine since we are in a JTA environment.

What you need to do is let the JTA TransactionManager do the management for you.

Code:
transactionTemplate.execute((TransactionCallback<Void>) transactionStatus1 -> {
   try(Session session1 = entityManagerFactory.unwrap(SessionFactory.class).openSession()) {
      transactionTemplate.execute((TransactionCallback<Void>) transactionStatus2 -> {
         try(Session session2 = entityManagerFactory.unwrap(SessionFactory.class).openSession()) {
         }
         return null;
      });
   }
   return null;
});


Which works like a charm. Use the Java EE application server to coordinate that for you.


Top
 Profile  
 
 Post subject: Re: Strange Behavior at Transaction Management after upgrade
PostPosted: Wed Oct 18, 2017 10:51 am 
Newbie

Joined: Wed Oct 18, 2017 5:13 am
Posts: 3
Thanks a lot for your quick response.

I tried the following snippet of code :

Session session1 = sessionFactory.openSession();
Bean b = new Bean();
b.setName("test");
session1.save(b);
session1.close();

and nothing has been inserted at the database

Also, I want to mention that the behavior mentioned at the original post was working before the upgrade.


Top
 Profile  
 
 Post subject: Re: Strange Behavior at Transaction Management after upgrade
PostPosted: Wed Oct 18, 2017 4:02 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
From the configs alone, I can't tell why this might happen, so you need to debug it and see what's causing the issue.


Top
 Profile  
 
 Post subject: Re: Strange Behavior at Transaction Management after upgrade
PostPosted: Thu Oct 19, 2017 5:23 am 
Newbie

Joined: Wed Oct 18, 2017 5:13 am
Posts: 3
We already debug our code carefully and also attache the source code of hibernate itself to the libs and debug inside it and found the following:

There is only one TransactionManager. Before beginning any transaction any call to transactionManager.getStatus() it returns "inactive" after beginning any transaction the status turns to "active" and we won't be able to begin any subsequent transactions. We tried also to disable the cache so we have different objects from the "TransactionManager" but also all the inquiries for the status after beginning the first one returns "active".


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.