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.  [ 8 posts ] 
Author Message
 Post subject: Which transaction strategy should i use
PostPosted: Mon Feb 11, 2008 8:27 am 
Newbie

Joined: Mon Feb 11, 2008 8:15 am
Posts: 7
Location: india
Hi,

I am having application with struts2 + mysql + hibernate simple DAO patter with Tomcat 5.x.

i want to use hibernate transaction,can anybody suggest which one is the best suitable strategy for my application.

My
hibernate.cfg.xml(using c3p0 connection pooling/plain jdbc connection)

Code:
<hibernate-configuration>

<session-factory>

   <property name="connection.username">uname</property>
   <property name="connection.password">passwd</property>
   <property name="connection.url">
      jdbc:mysql://localhost:3306/XXX
   </property>


   <property name="dialect">
      org.hibernate.dialect.MySQLDialect
   </property>

   <property name="connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <property name="hibernate.hbm2ddl.auto">update</property>
   <property name="hibernate.query.substitutions">
      true 1, false 0, yes 1, no 0
   </property>

   <property name="connection.provider_class">
      org.hibernate.connection.C3P0ConnectionProvider
   </property>

   <!-- configuration pool via c3p0-->
   <property name="c3p0.acquire_increment">1</property>
   <property name="c3p0.idle_test_period">100</property>
   <!-- seconds -->
   <property name="c3p0.max_size">100</property>
   <property name="c3p0.max_statements">0</property>
   <property name="c3p0.min_size">10</property>
   <property name="c3p0.timeout">100</property><!-- seconds -->
   <!-- DEPRECATED very expensive property name="c3p0.validate>-->

   <mapping resource="abc.hbm.xml" />
   <mapping resource="xyz.xml" />
        .....etc
</session-factory>

</hibernate-configuration>



My SessionFactory class.


public class HibernateSessionFactory {

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

private static Configuration configuration = new Configuration();


private static org.hibernate.SessionFactory sessionFactory;


private static String configFile = CONFIG_FILE_LOCATION;

private HibernateSessionFactory() {
}

public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

public static Configuration getConfiguration() {
return configuration;
}
}


My DataAccess class

public void saveOrUpdate(Customers transientInstance) throws Exception, Throwable {
getSession().saveOrUpdate(transientInstance);
getSession().flush();
}


Now problem is where can i place transaction code and by which strategy (JTA,plain jdbc,EJB/Cmt) ?

thanks in advanced
regards,
jignesh


Last edited by jignesh on Wed Feb 13, 2008 12:40 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Which transaction strategy should i use
PostPosted: Mon Feb 11, 2008 8:18 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
As I understand your application, local transactions are good enough, and you may use org.hibernate.transaction.JDBCTransactionFactory.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 1:07 am 
Newbie

Joined: Mon Feb 11, 2008 8:15 am
Posts: 7
Location: india
Hi,
I am using the simple local transaction strategy but it fails to make rollback on exception.

Here is my test case.

Ttp ttp = new Ttp();
ttp.setCity("private1");
ttp.setContact("contact1");
ttp.setCountry("country1");
ttp.setEmail("email1");
ttp.setName("name1");
org.hibernate.Transaction trx = null;

try {
trx = isp.services.IspDao.getTtpDao().getSession().beginTransaction();
isp.services.IspDao.getTtpDao().getSession().save(ttp);
int l =9/0;
trx.commit();
}
catch (Exception e) {
System.out.println("~~~~~~~~~~~~ROLLED BACK 2~~~~~~~~");
trx.rollback();
// TODO Auto-generated catch block
e.printStackTrace();
}

I have generated divide by zero exception and goes to catch block but it still saves the object into db.

Can u help me where can i miss the configuration.

Thanks,
jignesh


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 6:17 am 
Newbie

Joined: Mon Feb 11, 2008 8:15 am
Posts: 7
Location: india
One more thing perhaps is it ok or not.
Hibernate can not be able to rollback if you are using "identity " as generator class in mysql.

while with the same configuration but different generator class transaction,rollback works what they should be.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 2:21 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I can't think of anything with the information I have now. Can you show me what you see on the logs. Can you show me the hibernate config file you have now? and please use the code tags so that the post is formatted nicely. Im half blind :)

Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 12:25 am 
Newbie

Joined: Mon Feb 11, 2008 8:15 am
Posts: 7
Location: india
Sorry for the inconvenience,

hibernate.cfg.xml(using c3p0 connection pooling/plain jdbc connection)
Code:
<hibernate-configuration>

<session-factory>

<property name="connection.username">uname</property>
<property name="connection.password">passwd</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/XXX
</property>


<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>

<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>

<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.query.substitutions">
true 1, false 0, yes 1, no 0
</property>

<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>

<!-- configuration pool via c3p0-->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property>
<!-- seconds -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property><!-- seconds -->
<!-- DEPRECATED very expensive property name="c3p0.validate>-->

<mapping resource="abc.hbm.xml" />
<mapping resource="xyz.xml" />
.....etc
</session-factory>

</hibernate-configuration>

[/code]

My SessionFactory class.

Code:
public class HibernateSessionFactory {

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

private static Configuration configuration = new Configuration();

private static org.hibernate.SessionFactory sessionFactory;

private static String configFile = CONFIG_FILE_LOCATION;

private HibernateSessionFactory() {
}

public static Session getSession() throws HibernateException {
    Session session = (Session) threadLocal.get();

    if (session == null || !session.isOpen()) {
        if (sessionFactory == null) {
            rebuildSessionFactory();
        }
        session = (sessionFactory != null) ? sessionFactory.openSession(): null;
            threadLocal.set(session);
        }

    return session;
}

public static void rebuildSessionFactory() {
    try {
        configuration.configure(configFile);
        sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
        System.err.println("%%%% Error Creating SessionFactory %%%%");
        e.printStackTrace();
    }
}

public static void closeSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    threadLocal.set(null);

    if (session != null) {
        session.close();
    }
}

public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static void setConfigFile(String configFile) {
    HibernateSessionFactory.configFile = configFile;
    sessionFactory = null;
}

public static Configuration getConfiguration() {
    return configuration;
}
}


MyDataAccess class

Code:
public void saveOrUpdate(Customers transientInstance) throws Exception, Throwable {
    getSession().saveOrUpdate(transientInstance);
    getSession().flush();
}


br,
jignesh


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 8:53 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Sorry you basically repeated the first post. Show me the parts in hibernate configuration that sets up transaction stuff. However, I still have no explanation why identity setting is causing the problem.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 12:15 am 
Newbie

Joined: Mon Feb 11, 2008 8:15 am
Posts: 7
Location: india
Here is my test main class and i have generated the divide By Zero exception exactly after the saveOrUpdate method.
It should make rollback but it still saves the row using those data i have provided.

Code:
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Ttp ttp = new Ttp();
      ttp.setCity("private1");
      ttp.setContact("contact1");
      ttp.setCountry("country1");
      ttp.setEmail("email1");
      ttp.setName("name1");
      org.hibernate.Transaction trx = null;
      
      try {
         trx = isp.services.IspDao.getTtpDao().getSession().beginTransaction();
         isp.services.IspDao.getTtpDao().saveOrUpdate(ttp);
         int l =9/0;
         trx.commit();
      }
      catch (Exception e) {
         trx.rollback();
         e.printStackTrace();
      }
   }


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