Hi,
I was told like, our application should use JTA ( <property name="current_session_context_class">
jta</property> )
instead of thread(<property name="current_session_context_class">
thread</property> )
Initially(before JTA) the configuration is,
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@10.90.80.11:1521:orcl</property>
<property name="connection.username">tbauser</property>
<property name="connection.password">tbauser</property>
.
.
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="current_session_context_class">thread</property>
When we have the above configuraion our application is working fine.
-------
But when i changed to below configuration, it started to raise exception
(
nested exception is org.hibernate.HibernateException: Unable to locate current JTA transaction)
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@10.90.80.11:1521:orcl</property>
<property name="connection.username">tbauser</property>
<property name="connection.password">tbauser</property>
.
.
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="current_session_context_class">jta</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="transaction.manager_lookup">org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.WebSphereTransactionManagerLookup</property>
<property name="transaction.flush_before_completion">true</property>
I am using websphere 8.
I tried with by creating datasource in Server. That is also not working.
<property name="connection.datasource">jdbc:/tbisds</property>
Please let me know how to resolve this issue.
This is my HibernateSessionFactory.java
Code:
package com.tbis.core.util;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
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 static final Logger logger = Logger.getLogger(HibernateSessionFactory.class);
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
if (sessionFactory == null) {
rebuildSessionFactory();
}
Session session = (sessionFactory != null) ? sessionFactory.getCurrentSession(): null;
if(session==null || !session.isOpen()){
session=sessionFactory.openSession();
}
Transaction t=session.getTransaction();
if(t==null || !t.isActive()){
session.beginTransaction();
}
return session;
}
public static void commit() throws HibernateException {
if (sessionFactory == null) {
rebuildSessionFactory();
}
Session session = (sessionFactory != null) ? sessionFactory.getCurrentSession(): null;
if (session == null || !session.isOpen()) {
throw new HibernateException("No open Session available");
}
logger.debug("Fetching a session from Current Session");
Transaction t=session.getTransaction();
if(t==null || !t.isActive()){
throw new HibernateException("No Transaction available or Active");
}else{
t.commit();
closeSession();
}
}
public static void rollback() throws HibernateException {
if (sessionFactory == null) {
rebuildSessionFactory();
}
Session session = (sessionFactory != null) ? sessionFactory.getCurrentSession(): null;
if(session==null || !session.isOpen()){
throw new HibernateException("No open Session available");
}
Transaction t=session.getTransaction();
if(t==null || !t.isActive()){
throw new HibernateException("No Transaction available or Active");
}else{
t.rollback();
closeSession();
}
}
public static void rebuildSessionFactory() {
try {
configuration = new AnnotationConfiguration().configure();
sessionFactory = configuration
/* .setInterceptor(new AuditTrailInterceptor())*/
.buildSessionFactory();
} catch (Exception e) {
logger.error("Error at RebuildSessionFactory",e);
}
}
public static void closeSession() throws HibernateException {
if (sessionFactory == null) {
return;
}
Session session = (sessionFactory != null) ? sessionFactory.getCurrentSession() : null;
if (session == null || !session.isOpen()) {
return;
} else {
try {
session.close();
session=null;
} catch (Exception e) {
logger.error("session cannot be closed"+e.getMessage());
}
}
}
public static boolean isSessionTransactionEnabled(){
return getSessionFactory().getCurrentSession().getTransaction().isActive();
}
public static org.hibernate.SessionFactory getSessionFactory() {
if (sessionFactory == null) {
rebuildSessionFactory();
}
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
Thanks,
balachandar