-->
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.  [ 37 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Transaction management on multiple DB connections
PostPosted: Wed Jan 16, 2008 10:49 am 
Newbie

Joined: Wed Jan 16, 2008 10:42 am
Posts: 12
Hibernate version:3.2.5.ga

Hi,

If you want to manage transactions on multiple databases connections, can you do that with Hibernate?

For instance, I want to update data on both databases, but the first transaction on a DB has to be rollback after second transaction on the other DB fails. How can you handle this kind scenario?

Thanks in advance.

Brad


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Wed Jan 16, 2008 11:01 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You need to set up distributed transaction in your data sources. That usually involves using the XA version of drivers. However, this will impact performance.



Farzad-


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 10:37 am 
Newbie

Joined: Wed Jan 16, 2008 10:42 am
Posts: 12
Hi Farzad

Thanks for your quick answer and valuable information. I tried to implement it, however I got a error message
“org.hibernate.TransactionException: could not register synchronization”.
Would you correct me what I am wrong from the following?

Thanks again,
Brad

===Oracle JDBC Driver (XA) on Websphere datasource===
oracle.jdbc.xa.client.OracleXADataSource

===hibernate configuration===
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</property>
<property name="hibernate.connection.datasource">jdbc/dsXA1</property>

<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.CMTTransactionFactory
</property>
<property name="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
</property>

<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
</session-factory>
</hibernate-configuration>

===Java dao code===
for (int idx=0; idx<InitSessionFactory.NUMBER_OF_INSTATNCE; idx++)
{
try
{
Session session = InitSessionFactory.getCurrentSession(idx);
session.saveOrUpdate(obj);
}
catch ( Exception e )
{
e.printStackTrace();
log.error(": Exception on <updateTestOR>: " + e.getMessage());
}
}


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 10:41 am 
Newbie

Joined: Wed Jan 16, 2008 10:42 am
Posts: 12
If I use “JTATransactionFactory” in hibernate configuration, this error message is shown.
“org.hibernate.TransactionException: could not register synchronization with JTA TransactionManager”

<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
</property>


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 12:44 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
javara wrote:
“org.hibernate.TransactionException: could not register synchronization with JTA TransactionManager”


Can you give me the whole stack trace? What if you don't specify the factory class and only transaction manager lookup class?

And just my curiosity, you have two session factories right?


Farzad-


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 3:28 pm 
Newbie

Joined: Wed Jan 16, 2008 10:42 am
Posts: 12
And also, here is error stack when org.hibernate.transaction.CMTTransactionFactory is used.

0000002e SystemErr R org.hibernate.TransactionException: Could not register synchronization
at org.hibernate.transaction.CMTTransaction.registerSynchronization(CMTTransaction.java:159)
at org.hibernate.context.ThreadLocalSessionContext.currentSession(ThreadLocalSessionContext.java:78)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
at com.ibm.msp.webtim.hibernate.InitSessionFactory.getCurrentSession(InitSessionFactory.java:87)
at com.ibm.msp.webtim.dao.TestORDao.updateTestOR(TestORDao.java:59)
at webtest.actions.FirstAction.execute(FirstAction.java:37)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1212)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:629)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:2837)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:220)
at com.ibm.ws.webcontainer.VirtualHost.handleRequest(VirtualHost.java:204)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1681)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:77)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:421)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:367)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminaters(NewConnectionInitialReadCallback.java:201)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:103)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:548)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:601)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:934)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1021)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))
Caused by: org.hibernate.HibernateException: java.lang.reflect.InvocationTargetException
at org.hibernate.transaction.WebSphereExtendedJTATransactionLookup$TransactionManagerAdapter$TransactionAdapter.registerSynchronization(WebSphereExtendedJTATransactionLookup.java:165)
at org.hibernate.transaction.CMTTransaction.registerSynchronization(CMTTransaction.java:156)
... 28 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
at org.hibernate.transaction.WebSphereExtendedJTATransactionLookup$TransactionManagerAdapter$TransactionAdapter.registerSynchronization(WebSphereExtendedJTATransactionLookup.java:159)
... 29 more
Caused by: com.ibm.websphere.jtaextensions.NotSupportedException
at com.ibm.ws.jtaextensions.ExtendedJTATransactionImpl.registerSynchronizationCallbackForCurrentTran(ExtendedJTATransactionImpl.java:227)
... 35 more


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 3:57 pm 
Newbie

Joined: Wed Jan 16, 2008 10:42 am
Posts: 12
Here is SessionFactory java code which creates multiple SessionFactory, same as the size of CONFIG_FILE_LOCATIONS[].

private static synchronized void initSessionFactory(int idx)
{
if (sessionFactory[idx] == null)
{
try
{
cfg[idx] = new Configuration();
cfg[idx].configure(new URL (CONFIG_FILE_LOCATIONS[idx]));//Hibernate configuration files

String sessionFactoryJndiName
= cfg[idx].getProperty(Environment.SESSION_FACTORY_NAME);
if (sessionFactoryJndiName != null) {
cfg[idx].buildSessionFactory();
log.debug("get a jndi session factory");
sessionFactory[idx] = (SessionFactory) (new InitialContext())
.lookup(sessionFactoryJndiName);
} else{
log.debug("classic factory");
sessionFactory[idx] = cfg[idx].buildSessionFactory();
}

sessionFactory[idx] = cfg[idx].buildSessionFactory();
}
catch (Exception e)
{
log.error("%%%% Error Creating HibernateSessionFactory %%%%");
e.printStackTrace();
throw new HibernateException(
"Could not initialize the Hibernate configuration");
}
}
}


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 3:57 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What WAS version are you using?


Farzad-


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 4:00 pm 
Newbie

Joined: Wed Jan 16, 2008 10:42 am
Posts: 12
WAS 6.1
Hibernate 3.2.5
Oracle 9i


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 4:13 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
javara wrote:
WAS 6.1
Hibernate 3.2.5
Oracle 9i


According to WAS documents, the exception says:

Quote:
The exception is thrown by the transaction manager if an attempt is made to register a SynchronizationCallback in an environment or at a time when this function is not available.



This means your call is most likely not in an scope of a transaction at all, and as I can see from stack trace I don't see a point which implies creating a new transaction. It's a web call. You will need to demarcate this call in a transaction scope. You can either get a UserTransaction or get a framework like Spring to have it done for you.


Farzad-


Top
 Profile  
 
 Post subject: Re: Transaction management on multiple DB connections
PostPosted: Thu Jan 17, 2008 7:30 pm 
Newbie

Joined: Wed Jan 16, 2008 10:42 am
Posts: 12
Thanks Farzad

The issue has been solved with your idea.
UserTransaction is taken from Session bean (BMP) and it sets transaction scope in session bean method which calls Hibernate DAO class. The method of the DAO class updates data into multiple data source. It works fine (even I don’t do performance testing yet).

Here is hibernate configuration and EJB method for another people who try to find out a example code of distributed transaction management.

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</property>
<property name="hibernate.connection.datasource">jdbc/dsXA1</property>

<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.CMTTransactionFactory
</property>
<property name="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
</property>

<property name="show_sql">true</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Mapping files -->
<mapping resource="webtest.hbm.xml"/>

</session-factory>
</hibernate-configuration>


==EJB method (BMP)===
public void updateTestOR(TestOR obj) throws Exception
{
UserTransaction ut = getSessionContext().getUserTransaction();
try {
ut.begin();
TestORDao objDao = new TestORDao();
objDao.updateTestOR(obj);
ut.commit();
} catch (Exception e) {
ut.rollback();
}
}


Top
 Profile  
 
 Post subject: same issue
PostPosted: Mon Jan 28, 2008 12:55 pm 
Newbie

Joined: Fri Jan 25, 2008 3:14 pm
Posts: 16
I'm using Required level transactions and I get the same exception.
Since I'm using Container Managed Transactions and not not using bean managed transactions I can not do this:

Code:
UserTransaction ut = getSessionContext().getUserTransaction();
try {
ut.begin();
TestORDao objDao = new TestORDao();
objDao.updateTestOR(obj);
ut.commit();
} catch (Exception e) {
ut.rollback();


So how do I solve this in my situation?

Shawn


Top
 Profile  
 
 Post subject: Re: same issue
PostPosted: Mon Jan 28, 2008 12:57 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Has HB been configured with a JTA transaction manager?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 1:00 pm 
Newbie

Joined: Fri Jan 25, 2008 3:14 pm
Posts: 16
Yes

Code:
<bean id="hibernateProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="properties">
         <props>
            <prop key="hibernate.dialect">
               org.hibernate.dialect.DB2Dialect
            </prop>
            <prop key="hibernate.connection.url">jdbc/myDB2URL</prop>
            <prop key="hibernate.default_schema">DB2</prop>
            <prop key="hibernate.transaction.factory_class">
               org.hibernate.transaction.CMTTransactionFactory
            </prop>
            <prop key="hibernate.transaction.manager_lookup_class">
               org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
            </prop>
            <prop key="hibernate.transaction.auto_close_session">true</prop>
            <prop key="hibernate.transaction.flush_before_completion">
               true
            </prop>
            <prop key="hibernate.show_sql">false</prop>
         </props>
      </property>
   </bean>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 1:05 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What if you change the CMTTransactionFactory to a JTATransactionFactory? I don't recall the difference from top of my head but JTA would be a better fit I guess.


Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 37 posts ]  Go to page 1, 2, 3  Next

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.