-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate, Java Persistence -- Tran. rollback issue
PostPosted: Tue Mar 18, 2008 12:03 pm 
Newbie

Joined: Tue Mar 18, 2008 11:49 am
Posts: 4
Hi,

I am learning Java Persistence API provided by Hibernate. I am trying to run a simple test where I am making some changes in the database and rolling back the transaction. The test runs fine, but when I query DB I see all the changes made by the test. Somehow, rollback call doesn't do what it is supposed to do.

In below code, UserTestCase extends PersistenceBaseClass, which starts the transaction in setUp() method and rollbacks the transaction in tearDown() method. Thus, tx.rollback() method will be called after the execution of each test in UserTestCase class.


Source Code:

Code:
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import junit.framework.TestCase;


public class PersistenceBaseClass extends TestCase {

   private EntityManagerFactory emf;
   protected EntityManager em;
   protected EntityTransaction tx;

   public PersistenceBaseClass() {
      emf = Persistence.createEntityManagerFactory("s2app");
   }
   
   @Override
   protected void setUp() throws Exception {
      super.setUp();
      em = emf.createEntityManager();
      tx = em.getTransaction();
      tx.begin();
   }
   
   @Override
   protected void tearDown() throws Exception {
      super.tearDown();
      if(tx.isActive()) {
         // rollback the transaction so that no changes take place in DB
         tx.rollback();
      }
   }
}


public class UserTestCase extends PersistenceBaseClass {

   public void testCreateFind() throws Exception {
      
      User user = new User();
      user.setEmail("user1@s2app.com");
      user.setPassword("Password");
      em.persist(user);
      em.flush();
      
      User test = em.find(User.class, user.getEmail());
      assertNotNull(test);
      assertEquals(user,test);
   }
}


Can any please provide some assistance?

Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Hibernate, Java Persistence -- Tran. rollback issue
PostPosted: Tue Mar 18, 2008 12:09 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
how does the persistence.xml file look like?


Farzad-


Top
 Profile  
 
 Post subject: Re: Hibernate, Java Persistence -- Tran. rollback issue
PostPosted: Tue Mar 18, 2008 4:07 pm 
Newbie

Joined: Tue Mar 18, 2008 11:49 am
Posts: 4
farzad wrote:
how does the persistence.xml file look like?


Farzad-


Thanks for looking into it. Here, is my persistence.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
   <persistence-unit name="s2app">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.MySQLDialect" />
         <property name="hibernate.connection.driver_class"
            value="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource" />
         <property name="hibernate.connection.url"
            value="jdbc:mysql://localhost/s2app" />
         <property name="hibernate.connection.username" value="s2app" />
         <property name="hibernate.connection.password"
            value="s2app" />
         <property name="hibernate.hbm2ddl.auto" value="validate" />
         <property name="hibernate.archive.autodetection"
            value="class" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.format_sql" value="true" />
         
         <property name="hibernate.connection.autocommit" value="false"/>
      </properties>
   </persistence-unit>
</persistence>



Top
 Profile  
 
 Post subject: Re: Hibernate, Java Persistence -- Tran. rollback issue
PostPosted: Tue Mar 18, 2008 4:21 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
so far everything looks good to me. Can you also show me the log files at startup. Probably we can see something there. I also remember that a user complained his connection always have autocommit=true when managed with hibernate. I am not suggesting this is your problem but I am wondering if there is a combination of things that might cause the issue.



Farzad-


Top
 Profile  
 
 Post subject: Re: Hibernate, Java Persistence -- Tran. rollback issue
PostPosted: Tue Mar 18, 2008 4:36 pm 
Newbie

Joined: Tue Mar 18, 2008 11:49 am
Posts: 4
farzad wrote:
so far everything looks good to me. Can you also show me the log files at startup. Probably we can see something there. I also remember that a user complained his connection always have autocommit=true when managed with hibernate. I am not suggesting this is your problem but I am wondering if there is a combination of things that might cause the issue.



Farzad-


I tried many options but couldn't figure out the problem. Plus, this is as per the example in the Struts2 book. Below, is the text from the log file. autocommit is set to false.

Quote:
Mar 18, 2008 4:27:10 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
Mar 18, 2008 4:27:10 PM org.hibernate.connection.DriverManagerConnectionProvider close
INFO: cleaning up connection pool: jdbc:mysql://localhost/s2app
Mar 18, 2008 4:27:10 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
Mar 18, 2008 4:27:10 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
Mar 18, 2008 4:27:10 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource at URL: jdbc:mysql://localhost/s2app
Mar 18, 2008 4:27:10 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=s2app, password=****, autocommit=false, release_mode=auto}
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.0.51a
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.14 ( $Date: 2006-10-18 17:40:15 +0200 (Wed, 18 Oct 2006) $, $Revision: 5888 $ )
Mar 18, 2008 4:27:10 PM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
Mar 18, 2008 4:27:10 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
Mar 18, 2008 4:27:10 PM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Mar 18, 2008 4:27:10 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: enabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
Mar 18, 2008 4:27:10 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo


Top
 Profile  
 
 Post subject: Re: Hibernate, Java Persistence -- Tran. rollback issue
PostPosted: Tue Mar 18, 2008 4:49 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
The only thing I don't like here is hibernate.connection.driver_class=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. I don't know how a data source class is working in place of a Driver class. I do suggest that you replace that with a proper MySQL driver and see if transaction is working.



Farzad-


Top
 Profile  
 
 Post subject: Re: Hibernate, Java Persistence -- Tran. rollback issue
PostPosted: Tue Mar 18, 2008 8:11 pm 
Newbie

Joined: Tue Mar 18, 2008 11:49 am
Posts: 4
farzad wrote:
The only thing I don't like here is hibernate.connection.driver_class=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. I don't know how a data source class is working in place of a Driver class. I do suggest that you replace that with a proper MySQL driver and see if transaction is working.



Farzad-


Thanks for your time.

Changing driver name didn't solve the problem. :(

I update the driver version, changed the driver name but it didn't make any difference. I used MySql Java Connector version 5.1.5, and for Driver I tried both:
com.mysql.jdbc.Driver and
org.gjt.mm.mysql.Driver


When I set hibernate.connection.autocommit=false, I saw a warning in the log file.

WARNING: hibernate.connection.autocommit = false break the EJB3 specification


In case, I remove call to flush() method, everything works. But that is not the solution as it won't even try to synchronize with database.

Not sure what to do now.....


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