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