-->
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.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Tue Apr 13, 2010 3:46 am 
Newbie

Joined: Tue Apr 13, 2010 3:37 am
Posts: 9
Hi guys,

I am using Hibernate for my ORM needs with Tomcat as the application server. Since Tomcat doesnt have a default JTA offering, we are using JOTM to handle transactions.
But
- Commit and Rollback does not seem to happen with JOTM
- Is JOTM the best JTA implementation for Hibernate with Tomcat?
- What about BTM and Atomicos?
- Additionally I heard that JBOSS Transactions, which I opine would have been the perfect fit with Hibernate, does not work with Tomcat but only with JDBC. How far is this true?

Can any one suggest the best implementation for using JTA in Hibernate on Tomcat?

Thanks and Regards,
~Girish


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 3:55 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
- Commit and Rollback does not seem to happen with JOTM


This happens, when your DataSource is not JTA-aware.
(The DataSource XAResource has to be enlisted in the TransactionManager JOTM)

Quote:
- What about BTM and Atomicos?
- Additionally I heard that JBOSS Transactions


In following article you find examples for correctly using JTA with JOTM, BTM, Atomicos and JBossTM outside a EJB3Server.

http://community.jboss.org/wiki/ImplementingstandaloneJPAJTAHibernateapplicationoutsideJ2EEserverusingInfinispan2ndlevelcache

hope this helps


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 5:25 am 
Newbie

Joined: Tue Apr 13, 2010 3:37 am
Posts: 9
Thank you very much for your reply.
Just to clarify our scenario:
- We are not completely stand alone, we use Hibernate + JOTM on Tomcat 6.0.
- We have a transaction entry in our context.xml file
Code:
<Transaction factory="org.objectweb.jotm.UserTransactionFactory"
                 jotm.timeout="60"/>

- We are also getting the hibernate session from JNDI on tomcat via a custom resource handler.
- Our hibernate cfg file looks like this

Code:
<hibernate-configuration>
    <session-factory name="hibernate/YouDBSessionFactory">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">wewillwin</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/YouDB</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="current_session_context_class">jta</property>
        <property name="show_sql">true</property>
        <property name="connection.release.mode">auto</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JOTMTransactionManagerLookup</property>
   <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
        <property name="hibernate.jndi.class">org.ow2.carol.jndi.spi.MultiOrbInitialContextFactory</property>

- Our persist logic looks like this
Code:
UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
try{
ut.begin();

GenericHibernateDAO<BasicProfile, Long> dao = new BasicProfileDAO();

BasicProfile bp = new BasicProfile();
bp.setFirstName("Eric");
bp.setLastName("Thiery");
bp.setCountryCode("IN");

dao.persist(bp);

ut.commit();

}catch (Exception ex){

   out.println(ex.getMessage());
   ut.rollback();
}


- But commit/rollback does not happen.

Can you pls advise us here.


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 8:24 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
- We are not completely stand alone, we use Hibernate + JOTM on Tomcat 6.0.


Also I deploy my application as servlet on tomcat.
From this point of view this hiberante configuration is to consider standalone anyway
as it is not deployed in a managed ebj3 environment.

Code:
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">wewillwin</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/YouDB</property>
        <property name="hibernate.connection.username">root</property>


These properties above belong to a JDBC configuration (JDBCTransactionFactory) and have
no sense in a JTA environment (JTATransactionFactory).
You should remove this entries.
In a JTA environment YOU MUST REFER TO A DATASOURCE instead in your configuration.


Code:
<session-factory name="hibernate/YouDBSessionFactory">


If I remember right, hibernate tries to bind the sessionfactory to JDNI if you specify a session-factory name.
You don't need that, and moreover the binding can/will easily fail with certain JNDI-implementations.
Therefore: don't specify a name for the session factory

Code:
<session-factory>


Quote:
- But commit/rollback does not happen.


As I explained in the posting before, it is of fundamental importance to get the datasource jta-aware,
otherwise only DML-statements are propagated to the database but no commit/rollback.
Please take a deeper look a the examples in the linked article, especially the point where the datasource is defined.

All what's different in the examples in comparison to your environment,
is that they use JPA approach (= persistence.xml configuration file) whilst you use the classical hibernate configuration.
(just look for, how to refer a datasource in classical hibernate configuration)


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 8:45 am 
Newbie

Joined: Tue Apr 13, 2010 3:37 am
Posts: 9
thank you very much for your responses. Its adding a lot of value at some critical stages of ours.

- As per your suggestion, we changed the hibernate configuration to refer to a datasource

Code:
<hibernate-configuration>
    <session-factory name="hibernate/YouDBSessionFactory">
       
       <property name="hibernate.connection.datasource">java:/comp/env/jdbc/LocalYouDB</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.connection.release.mode">auto</property>
         <property name="current_session_context_class">jta</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JOTMTransactionManagerLookup</property>
      <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
      <property name="hibernate.jndi.class">org.ow2.carol.jndi.spi.MultiOrbInitialContextFactory</property>
      <property name="jta.UserTransaction">java:comp/UserTransaction</property>
       


- In our context.xml we have this piece of code that tells hibernate to bind the session factory to JNDI
Code:
<Resource name="hibernate/YouDBSessionFactory"
                   auth="Container"
                 type="org.hibernate.SessionFactory"
                 factory="me.wulfpak.core.HibernateSessionFactoryTomcatResourceFactory"
                 configuration="youdb.cfg.xml"/>



- Now Commit works, but Rollback fails. Any further insights on this?


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 8:50 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
How does the rollback fail. Is there an exception with stacktrace?

By the way: Why you intend to use JTA approach instead to use the default JDBC approach (JDBCTransactionFactory)?


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 8:58 am 
Newbie

Joined: Tue Apr 13, 2010 3:37 am
Posts: 9
Quote:
How does the rollback fail. Is there an exception with stacktrace?

There is no exception thrown. But it commits the data to the MySQL database.

Quote:
By the way: Why you intend to use JTA approach instead to use the default JDBC approach (JDBCTransactionFactory)?

We are using the JTA approach since we have 2 databases to work with. And this is our persistence code.

Code:
UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
try{
ut.begin();

GenericHibernateDAO<BasicProfile, Long> dao = new BasicProfileDAO();

BasicProfile bp = new BasicProfile();
bp.setFirstName("Eric");
bp.setLastName("Thiery");
bp.setCountryCode("IN");

dao.persist(bp);

ut.rollback();

}catch (Exception ex){

   out.println(ex.getMessage());
   ut.rollback();
}


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 9:15 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
If I remember you can also activate some loggings on JOTM.
Check the logs and ensure that there it no any auto-commit happening.

Quote:
We are using the JTA approach since we have 2 databases to work with.


The fact alone, that you have 2 databases to work with, does not mandatory require integration with JTA.
Only if you need also make both database participate to the same transaction, then you need JTA
(with fully XA-compliant jdbc-driver supporting the 2phase-commit).
Do you need both database participate to the same transaction ?


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 9:19 am 
Newbie

Joined: Tue Apr 13, 2010 3:37 am
Posts: 9
1. I will check the logs in JOTM; not sure how to do it but will find out.

2. Yes we have 2 databases participating in the same transaction


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 9:56 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
2. Yes we have 2 databases participating in the same transaction


Ok.
One thing: Don't use XAPool as I used in my examples (wrapping non-xa-datasources as xa-datasource).

You probably have to use following as DataSource Classname if you use Mysql databases:
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource

Please see also http://docs.sun.com/app/docs/doc/819-3658/gbsor?a=view
and http://dev.mysql.com/doc/refman/5.5/en/xa-restrictions.html


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Wed Apr 14, 2010 1:49 pm 
Newbie

Joined: Tue Apr 13, 2010 3:37 am
Posts: 9
thanks for your reply. As per your advice I have changed the data source in Context.xml
Code:
<Resource name="jdbc/LocalYouDB"
              auth="Container" [color=#BF0000]type="javax.sql.XADataSource" factory="com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory"[/color]
            user="root"
            password="password"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/youdb"
            removeAbandoned="true"
            removeAbandonedTimeout="120"
            logAbandoned="true"
            maxWait="60"
            maxActive="8"
            maxIdle="4" />


I also changed web.xml to:
Code:
<resource-ref>
    <description>JDBC Connection to YouDB (Local)</description>
    <res-ref-name>jdbc/LocalYouDB</res-ref-name>
    <res-type>javax.sql.XADataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>


I have mysql-connector-java-5.0.8-bin.jar in my class path.

My hibernate.cfg.xml is:
Quote:
<session-factory>

<property name="hibernate.connection.datasource">java:/comp/env/jdbc/LocalYouDB</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.release.mode">auto</property>
<property name="current_session_context_class">jta</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JOTMTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.jndi.class">org.ow2.carol.jndi.spi.MultiOrbInitialContextFactory</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>


I get a weird "Could not find datasource: java:/comp/env/jdbc/LocalYouDB". Any idea if I am making a mistake somewhere?


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Thu Apr 15, 2010 2:03 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I get a weird "Could not find datasource: java:/comp/env/jdbc/LocalYouDB".


Seems that the step is missing which binds the datasource to JNDI.
(If you attach the complete stacktrace of the exception, then I could be more sure in the statement).
As I know tomcat offers a JNDI service, but it's some kind of read-only, so I'm not sure if it will work at all.
Anyway, If you don't are able to configure Tomcat in a way that it properly starts the datasource binding it to JNDI,
then as last resort you still can do start the datasource (and binds it to JNDI) manually in your hibernate-servlet similar as I did in the examples.


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Thu Apr 15, 2010 2:18 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
It could also happen that tomcat sucessfully binds the datasource (please check it in the tomcat logs),
but hibernate isn't able to resolve the datasource through jndi because using another jndi implementation.
In such case you must specify the proper jndi implementation in your hibernate configuration file:

Code:
<property name="hibernate.jndi.class">jndi-implementation-of-tomcat<property/>   


(Unfortunately from Tomcat's documentation I was not able to find out ont the fly which implementation of jndi it is using per default.)


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Thu Apr 15, 2010 10:24 am 
Newbie

Joined: Tue Apr 13, 2010 3:37 am
Posts: 9
thanks so much for replying and helping out.

As per your advice, I changed the Datasource in JNDI to MySQLXADatasource.

Still the commit does not happen :( my tx.rollback() does not work. Not sure why.
This is taking away my sleep. See details below:

1. In context.xml
Quote:
<Resource name="jdbc/LocalYouDB"
auth="Container"
type="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"
factory="com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory"
user="root"
password="password"
explicitUrl="true"
url="jdbc:mysql://localhost:3306/youdb?autoReconnect=true"
removeAbandoned="true"
removeAbandonedTimeout="120"
logAbandoned="true"
maxWait="60"
maxActive="8"
maxIdle="4" />


2. In Web.xml:
Code:
  <resource-ref>
    <description>JDBC Connection to YouDB (Local)</description>
    <res-ref-name>jdbc/LocalYouDB</res-ref-name>
    <res-type>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>


3. The hibernate.cfg.xml file is:
Code:
<hibernate-configuration>
    <session-factory>
       
       <property name="hibernate.connection.datasource">java:/comp/env/jdbc/LocalYouDB</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.connection.release.mode">auto</property>
       
<property name="current_session_context_class">jta</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JOTMTransactionManagerLookup</property>
      <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
      <property name="hibernate.jndi.class">org.ow2.carol.jndi.spi.MultiOrbInitialContextFactory</property>
      
      <property name="jta.UserTransaction">java:comp/UserTransaction</property>


Is there anyway that I can send you the WAR / show you the code through a conf call?


Top
 Profile  
 
 Post subject: Re: Hibernate with JOTM on Tomcat - issues with Commit and Rollb
PostPosted: Thu Apr 15, 2010 10:45 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I'm very busy now and whole next week I'm in vacation, sorry.
Try to startup the datasource manually in your servlet (as I do in the examples) , if tomcat is not able to do it for you.
The examples in my article are working, I tested them all and verified also that commit and rollback is working.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  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.