-->
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.  [ 1 post ] 
Author Message
 Post subject: Question about Generic DAO and JTA
PostPosted: Mon Apr 24, 2006 2:27 pm 
Newbie

Joined: Mon Apr 17, 2006 10:58 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

3.1

Mapping documents:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="hibernate/primarySessionFactory">

<property name="hibernate.cglib.use_reflection_optimizer">false</property>

<property name="hibernate.connection.datasource">java:comp/env/jdbc/myDS</property>
<property name="hibernate.connection.username">myUser</property>
<property name="hibernate.connection.password">myPasswd</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JOTMTransactionManagerLookup</property>

<mapping resource="com/mycomp/myapp/entity/User.hbm.xml" />
<mapping resource="com/mycomp/myapp/entity/UserDevice.hbm.xml" />
<mapping resource="com/mycomp/myapp/entity/ServiceProvider.hbm.xml" />
<mapping resource="com/mycomp/myapp/entity/ContentRating.hbm.xml"/>
<mapping resource="com/mycomp/myapp/entity/Country.hbm.xml" />
<mapping resource="com/mycomp/myapp/entity/Address.hbm.xml" />

</session-factory>
</hibernate-configuration>

Code between sessionFactory.openSession() and session.close():

I haven't done explicit session.close() after I opened the session and that is one of my questions related to use of GenericDAO in a JTA environment.

Full stack trace of any exception that occurs:


Name and version of the database you are using:

MySQL 4.1.11

The generated SQL (show_sql=true):

The SQL doesn't seem to be a problem but I can post it if there are specific queries anyone is interested in.

Debug level Hibernate log excerpt:

There is quite a bit of logs and this posting is pretty long without logs so I'll post any snippets per request.

My application uses JOTM and Tomcat 5.0. I wrote my DAOs following the approach described in http://www.hibernate.org/328.html. Now I started testing things and need some clarification.

The wrote a simple servlet and inside the doGet method I have the couple DAO, one to create a user and another to update its status (the code snippet is provided below). Both actions were successfully completed. While examining logs I noticed that the Hibernate session was never closed and that when I invoked the test servlet again I got the same session that I used in the first call.

Given that ThreadLocal is used to store sessions I can see how this happened. However, I thought that the session will get closed when the JOTM transaction gets committed and that my new transaction, even if started in the same thread would get a new Hibernate session. That did not happen according to the logs.

I probably wouldn't not have been as concerned had I not run into a case where I got a NonUniqueObjectException while adding my test cases. It's quite likely that my test case is not properly written but that just increased my preference for the Hibernate session to be closed when a transaction is committed.

While researching this forum and reviewing the documentation I noticed couple properties, transaction.auto_close_session and current_session_context_class, and added them to my Hibernate configuration file (
<property name="transaction.auto_close_session">true</property>
<property name="current_session_context_class">jta</property> ).

With those settings I was able to add my user object but the update status test failed with a null pointer exception. I hoped that because the session get automatically closed when the add user transaction completed and explicitely asked for a new DAO, I would get a new Hibernate session. However, it seems that the automatic session closure didn't disassociate the session from the thread and I got the exception. For what's worth it here's the exception stack trace:

java.lang.NullPointerException
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:442)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:93)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:86)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:62)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:326)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:202)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1111)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
at com.mycomp.myapp.dao.hibernate.test.UserDAOHBImpl.updateStatus(UserDAOHBImpl.java:238)
at com.mycomp.myapp.servlet.TestServlet.testHBUser(TestServlet.java:1261)
at com.mycomp.myapp.servlet.TestServlet.testHibernate(TestServlet.java:790)
at com.mycomp.myapp.servlet.TestServlet.doGet(TestServlet.java:108)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:595)


Here are my question:

1. What is the recommended approach for closing a Hibernate session when using Generic DAO?

2. So far every single user's request can be mapped to a single transaction. However, if I ever run into a case where a single user's request translates into multiple transactions what do I need to change?

3. I would like to ditch JOTM down the road. What is the recommended approach? Use Spring (I have very limitted experience with that), use JDBC transaction management or consider something even more heavy-weight (switch from Tomcat to JBoss for example)? Speaking of JBoss, is Hibernate going to provide support for JBoss Transactions (Arjuna)? Has anybody had any experience with Arjuna's TM before it was acquired by JBoss?

The sample code that I used to test my DAOs:

String jndiName = "hibernate/primarySessionFactory";
Context ctx = new InitialContext();
UserTransaction userTx = (UserTransaction) ctx.lookup(getDataSource());

userTx.begin();
UserDAO avUserDAO = HibernateDAOFactory.getInstance().getUserDAO(
jndiName);

// -- Add user
User avUser = new User();

avUser.setUserName("HBTestUser");
avUser.setPassword("abc");
avUser.setCreationDate(new Timestamp(System.currentTimeMillis()));
avUser.setStatus(EntityStatus.SUSPENDED);

ServiceProvider serviceProvider = new ServiceProvider(1);
avUser.setServiceProvider(serviceProvider);

DateOfBirth dob = new DateOfBirth(8, 8, 1971);
avUser.setDob(dob);

PhoneNumber phoneNumber = new PhoneNumber(1, 444, 4444444);
avUser.setPhoneNumber(phoneNumber);

DriversLicense driversLicense = new DriversLicense("66666666", "BC");
avUser.setDriversLicense(driversLicense);
addedUser = avUserDAO.add(avUser);

userTx.commit();

// -- Update status

userTx.begin();
avUserDAO = HibernateDAOFactory.getInstance().getUserDAO(jndiName);

avUser.setStatus(EntityStatus.ACTIVE);
avUserDAO.updateStatus(avUser);

userTx.commit();


The abstract DAOFactory I used:

public abstract class DAOFactory {
public abstract UserDAO getUserDAO();
}

Hibernate DAO Factory code:

public class HibernateDAOFactory extends DAOFactory {

private static HibernateDAOFactory instance = null;

public static synchronized HibernateDAOFactory getInstance() {
if (instance == null) {
instance = new HibernateDAOFactory();
}

return instance;
}

private HibernateDAOFactory() {
}


public UserDAO getUserDAO() {
return new UserDAOHBImpl(getCurrentSession());
}

public UserDAO getUserDAO(String jndiName) {
return new UserDAOHBImpl(getCurrentSession(jndiName));
}

protected Session getCurrentSession() {
// Get a Session and begin a database transaction. If the current
// thread/EJB already has an open Session and an ongoing Transaction,
// this is a no-op and only returns a reference to the current Session.
HibernateUtil2.beginTransaction();
return HibernateUtil2.currentSession();
}

protected Session getCurrentSession(String jndiName) {
// Get a Session and begin a database transaction. If the current
// thread/EJB already has an open Session and an ongoing Transaction,
// this is a no-op and only returns a reference to the current Session.
HibernateUtil2.beginTransaction(jndiName);
return HibernateUtil2.currentSession(jndiName);
}
}

I use slightly modified version of HibernateUtil from that deals with 2 different datasources. The same error occurred when I used the HibernateUtil that uses a single source.

public class HibernateUtil2 {

private static Log log = LogFactory.getLog(HibernateUtil2.class);

private static final String INTERCEPTOR_CLASS = "hibernate.util.interceptor_class";

private static final int DEF_NUM_DATA_SOURCES = 2;

private static final int PRIMARY_DS_INDEX = 0;

private static final int SECONDARY_DS_INDEX = 1;

private static Configuration[] configs = new Configuration[DEF_NUM_DATA_SOURCES];

private static SessionFactory[] sessionFactories = new SessionFactory[DEF_NUM_DATA_SOURCES];

public static final String PRIMARY_SESSION_FACTORY_JNDI = "hibernate/primarySessionFactory";

public static final String SECONDARY_SESSION_FACTORY_JNDI = "hibernate/secondarySessionFactory";

private static ThreadLocal[] threadSessions = new ThreadLocal[DEF_NUM_DATA_SOURCES];

private static ThreadLocal[] threadTransactions = new ThreadLocal[DEF_NUM_DATA_SOURCES];

private static boolean useThreadLocal = true;

static {
initThreadLocal();

try {
buildConfig(PRIMARY_DS_INDEX, "hibernate-primary.cfg.xml");
} catch (Throwable t) {
log.error("Failed to initialize primary config", t);
}
try {
buildConfig(SECONDARY_DS_INDEX, "hibernate-secondary.cfg.xml");
} catch (Throwable t) {
log.error("Failed to initialize secondary config", t);
}
}

private static void initThreadLocal() {
threadSessions[PRIMARY_DS_INDEX] = new ThreadLocal();
threadSessions[SECONDARY_DS_INDEX] = new ThreadLocal();
threadTransactions[PRIMARY_DS_INDEX] = new ThreadLocal();
threadTransactions[SECONDARY_DS_INDEX] = new ThreadLocal();
}

private static void buildConfig(int cfgIndex, String cfgFileName) {
// Create the initial SessionFactory from the default configuration
// files

try {

// Replace with Configuration() if you don't use annotations or JDK
// 5.0
// configuration = new AnnotationConfiguration();
configs[cfgIndex] = new Configuration();

// Read not only hibernate.properties, but also hibernate.cfg.xml
configs[cfgIndex].configure(cfgFileName);

// Assign a global, user-defined interceptor with no-arg constructor
String interceptorName = configs[cfgIndex]
.getProperty(INTERCEPTOR_CLASS);
if (interceptorName != null) {
Class interceptorClass = HibernateUtil.class.getClassLoader()
.loadClass(interceptorName);
Interceptor interceptor = (Interceptor) interceptorClass
.newInstance();
configs[cfgIndex].setInterceptor(interceptor);
}

// Disable ThreadLocal Session/Transaction handling if CMT is used
if (org.hibernate.transaction.CMTTransactionFactory.class
.getName()
.equals(
configs[cfgIndex] .getProperty(Environment.TRANSACTION_STRATEGY)))
useThreadLocal = false;

if (configs[cfgIndex].getProperty(Environment.SESSION_FACTORY_NAME) != null) {
// Let Hibernate bind it to JNDI
configs[cfgIndex].buildSessionFactory();
} else {
// or use static variable handling
sessionFactories[cfgIndex] = configs[cfgIndex]
.buildSessionFactory();
}

} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
log.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}

public static Configuration getConfiguration(String jndiName) {
Configuration config = null;
if (PRIMARY_SESSION_FACTORY_JNDI.equals(jndiName)) {
config = configs[PRIMARY_DS_INDEX];
} else if (SECONDARY_SESSION_FACTORY_JNDI.equals(jndiName)) {
config = configs[SECONDARY_DS_INDEX];
} else {
/*
* @TODO Log error
*/
}

return config;
}

public static SessionFactory getSessionFactory(String jndiName) {
SessionFactory sf = null;
Configuration configuration = getConfiguration(jndiName);

String sfName = configuration
.getProperty(Environment.SESSION_FACTORY_NAME);
if (sfName != null) {
log.debug("Looking up SessionFactory in JNDI.");
try {
sf = (SessionFactory) new InitialContext().lookup(sfName);
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
} else {
if (PRIMARY_SESSION_FACTORY_JNDI.equals(jndiName)) {
sf = sessionFactories[PRIMARY_DS_INDEX];
} else if (SECONDARY_SESSION_FACTORY_JNDI.equals(jndiName)) {
sf = sessionFactories[SECONDARY_DS_INDEX];
} else {
/*
* @TODO Log error
*/
}
}
if (sf == null)
throw new IllegalStateException("SessionFactory not available.");
return sf;
}

public static void shutdown() {
log.debug("Shutting down Hibernate.");

// Close caches and connection pools
getSessionFactory(PRIMARY_SESSION_FACTORY_JNDI).close();
getSessionFactory(SECONDARY_SESSION_FACTORY_JNDI).close();

// Clear static variables
configs[PRIMARY_DS_INDEX] = null;
configs[SECONDARY_DS_INDEX] = null;
sessionFactories[PRIMARY_DS_INDEX] = null;
sessionFactories[SECONDARY_DS_INDEX] = null;

// Clear ThreadLocal variables
threadSessions[PRIMARY_DS_INDEX].set(null);
threadSessions[SECONDARY_DS_INDEX].set(null);
threadTransactions[PRIMARY_DS_INDEX].set(null);
threadTransactions[SECONDARY_DS_INDEX].set(null);
}

public static void rebuildSessionFactory() {
log.debug("Using current Configuration for rebuild.");
rebuildSessionFactory(PRIMARY_DS_INDEX);
rebuildSessionFactory(SECONDARY_DS_INDEX);
}

public static void rebuildSessionFactory(int index) {
Configuration cfg = configs[index];

log.debug("Rebuilding the SessionFactory from given Configuration.");

synchronized (sessionFactories[index]) {
if (sessionFactories[index] != null
&& !sessionFactories[index].isClosed()) {
sessionFactories[index].close();
}
if (cfg.getProperty(Environment.SESSION_FACTORY_NAME) != null) {
cfg.buildSessionFactory();
} else {
sessionFactories[index] = cfg.buildSessionFactory();
}

configs[index] = cfg;
}
}

public static ThreadLocal getThreadLocalSession(String jndiName) {
ThreadLocal threadSession = null;
if (PRIMARY_SESSION_FACTORY_JNDI.equals(jndiName)) {
threadSession = threadSessions[PRIMARY_DS_INDEX];
} else if (SECONDARY_SESSION_FACTORY_JNDI.equals(jndiName)) {
threadSession = threadSessions[SECONDARY_DS_INDEX];
} else {
/*
* @TODO Log error
*/
}

return threadSession;
}

public static ThreadLocal getThreadLocalTransaction(String jndiName) {
ThreadLocal threadTx = null;
if (PRIMARY_SESSION_FACTORY_JNDI.equals(jndiName)) {
threadTx = threadTransactions[PRIMARY_DS_INDEX];
} else if (SECONDARY_SESSION_FACTORY_JNDI.equals(jndiName)) {
threadTx = threadTransactions[SECONDARY_DS_INDEX];
} else {
/*
* @TODO Log error
*/
}

return threadTx;
}

public static Session currentSession() {
return currentSession(PRIMARY_SESSION_FACTORY_JNDI);
}

public static Session currentSession(String jndiName) {
log.debug("Get current session [JNDI name: " + jndiName + "]");

if (useThreadLocal) {
ThreadLocal threadSession = getThreadLocalSession(jndiName);

Session s = (Session) threadSession.get();
if (s == null) {
log.debug("Opening new Session for this thread.");
s = getSessionFactory(jndiName).openSession();
log.debug("Opened new Session for this thread: " + s);
threadSession.set(s);
} else {
log
.debug("Current session is already bound to this thread [JNDI name: "
+ jndiName + "]: " + s);
}
return s;
} else {
return getSessionFactory(jndiName).getCurrentSession();
}
}

public static void closeSession() {
log.debug("Close session");

if (useThreadLocal) {
ThreadLocal threadSession = getThreadLocalSession(PRIMARY_SESSION_FACTORY_JNDI);
ThreadLocal threadTransaction = getThreadLocalTransaction(PRIMARY_SESSION_FACTORY_JNDI);

Session s = (Session) threadSession.get();
threadSession.set(null);
Transaction tx = (Transaction) threadTransaction.get();
if (tx != null && (!tx.wasCommitted() || !tx.wasRolledBack())) {
throw new IllegalStateException(
"Closing Session but Transaction still open!");
}

if (s != null && s.isOpen()) {
log.debug("Closing Session of this thread: " + s);
s.close();
} else {
log.debug("Session is already closed: " + s);
}
} else {
log.warn("Using CMT/JTA, intercepted superfluous close call.");
}
}

public static void closeSession(String jndiName) {

log.debug("Close session [JNDI name: " + jndiName + "]");

if (useThreadLocal) {
ThreadLocal threadSession = getThreadLocalSession(jndiName);
ThreadLocal threadTransaction = getThreadLocalTransaction(jndiName);

Session s = (Session) threadSession.get();
threadSession.set(null);
Transaction tx = (Transaction) threadTransaction.get();
if (tx != null && (!tx.wasCommitted() || !tx.wasRolledBack())) {
throw new IllegalStateException(
"Closing Session but Transaction still open!");
}

if (s != null && s.isOpen()) {
log.debug("Closing Session of this thread: " + s);
s.close();
} else {
log.debug("Session is already closed [JNDI name = " + jndiName
+ "]: " + s);
}
} else {
log.warn("Using CMT/JTA, intercepted superfluous close call.");
}
}

public static void beginTransaction() {
log.debug("Begin transaction");

if (useThreadLocal) {
ThreadLocal threadTransaction = getThreadLocalTransaction(PRIMARY_SESSION_FACTORY_JNDI);

Transaction tx = (Transaction) threadTransaction.get();
if (tx == null) {
log.debug("Starting new database transaction in this thread.");
// tx = getCurrentSession().beginTransaction();
tx = currentSession().beginTransaction();
threadTransaction.set(tx);
} else {
log.debug("Transaction is already bound to this thread");
}
} else {
log.warn("Using CMT/JTA, intercepted superfluous tx begin call.");
}
}

public static void beginTransaction(String jndiName) {
log.debug("Begin transaction [JNDI name = " + jndiName + "]");

if (useThreadLocal) {
ThreadLocal threadTransaction = getThreadLocalTransaction(jndiName);

Transaction tx = (Transaction) threadTransaction.get();
if (tx == null) {
log.debug("Starting new database transaction in this thread.");
// tx = getCurrentSession().beginTransaction();
tx = currentSession(jndiName).beginTransaction();
threadTransaction.set(tx);
} else {
log
.debug("Transaction is already bound to this thread [JNDI name = "
+ jndiName + "]: " + tx);
}
} else {
log.warn("Using CMT/JTA, intercepted superfluous tx begin call.");
}
}

public static void commitTransaction() {
log.debug("Commit transaction");

if (useThreadLocal) {
ThreadLocal threadTransaction = getThreadLocalTransaction(PRIMARY_SESSION_FACTORY_JNDI);

Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
log
.debug("Committing database transaction of this thread: "
+ tx);
tx.commit();
}
threadTransaction.set(null);
} catch (RuntimeException ex) {
log.error(ex);
rollbackTransaction();
throw ex;
}
} else {
log.warn("Using CMT/JTA, intercepted superfluous tx commit call.");
}
}

public static void commitTransaction(String jndiName) {
log.debug("Commit transaction [JNDI name = " + jndiName + "]");

if (useThreadLocal) {
ThreadLocal threadTransaction = getThreadLocalTransaction(jndiName);

Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
log
.debug("Committing database transaction of this thread [JNDI name = "
+ jndiName + "]: " + tx);
tx.commit();
}
threadTransaction.set(null);
} catch (RuntimeException ex) {
log.error(ex);
rollbackTransaction();
throw ex;
}
} else {
log.warn("Using CMT/JTA, intercepted superfluous tx commit call.");
}
}

public static void rollbackTransaction() {
log.debug("Rollback transaction");

if (useThreadLocal) {
ThreadLocal threadTransaction = getThreadLocalTransaction(PRIMARY_SESSION_FACTORY_JNDI);

Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
log
.debug("Trying to rollback database transaction of this thread : "
+ tx);
tx.rollback();
log.debug("Database transaction rolled back.");
}
} catch (RuntimeException ex) {
throw new RuntimeException(
"Might swallow original cause, check ERROR log!", ex);
} finally {
closeSession();
}
} else {
log
.warn("Using CMT/JTA, intercepted superfluous tx rollback call.");
}
}

public static void rollbackTransaction(String jndiName) {
log.debug("Rollback transaction [JNDI name = " + jndiName + "]");

if (useThreadLocal) {
ThreadLocal threadTransaction = getThreadLocalTransaction(jndiName);

Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
log
.debug("Trying to rollback database transaction of this thread [JNDI name = "
+ jndiName + "]: " + tx);
tx.rollback();
log.debug("Database transaction rolled back [JNDI name = "
+ jndiName + "]: " + tx);
}
} catch (RuntimeException ex) {
throw new RuntimeException(
"Might swallow original cause, check ERROR log!", ex);
} finally {
closeSession();
}
} else {
log
.warn("Using CMT/JTA, intercepted superfluous tx rollback call.");
}
}

public static void reconnect(Session session) {
log.debug("Reconnect session: " + session);

if (useThreadLocal) {
ThreadLocal threadSession = getThreadLocalSession(PRIMARY_SESSION_FACTORY_JNDI);

log.debug("Reconnecting Session to this thread: " + session);
session.reconnect();
threadSession.set(session);
} else {
log
.error("Using CMT/JTA, intercepted not supported reconnect call.");
}
}

public static Session disconnectSession() {
log.debug("Disconnect session");

if (useThreadLocal) {
ThreadLocal threadTransaction = getThreadLocalTransaction(PRIMARY_SESSION_FACTORY_JNDI);
ThreadLocal threadSession = getThreadLocalSession(PRIMARY_SESSION_FACTORY_JNDI);

Transaction tx = (Transaction) threadTransaction.get();
if (tx != null && (!tx.wasCommitted() || !tx.wasRolledBack())) {
throw new IllegalStateException(
"Disconnecting Session but Transaction still open: "
+ tx);
}
// Session session = getCurrentSession();
Session session = currentSession();
threadSession.set(null);
if (session.isConnected() && session.isOpen()) {
log.debug("Disconnecting Session from this thread: " + session);
session.disconnect();
}
return session;
} else {
log
.error("Using CMT/JTA, intercepted not supported disconnect call.");
return null;
}
}

public static Session disconnectSession(String jndiName) {
log.debug("Disconnect session [JNDI name = " + jndiName + "]");

if (useThreadLocal) {
ThreadLocal threadTransaction = getThreadLocalTransaction(jndiName);
ThreadLocal threadSession = getThreadLocalSession(jndiName);

Transaction tx = (Transaction) threadTransaction.get();
if (tx != null && (!tx.wasCommitted() || !tx.wasRolledBack())) {
throw new IllegalStateException(
"Disconnecting Session but Transaction still open!");
}

// Session session = getCurrentSession();
Session session = currentSession();
threadSession.set(null);

if (session.isConnected() && session.isOpen()) {
log
.debug("Disconnecting Session from this thread [JNDI name = "
+ jndiName + "]: " + session);
session.disconnect();
}
return session;
} else {
log
.error("Using CMT/JTA, intercepted not supported disconnect call.");
return null;
}
}

public static void registerInterceptorAndRebuild(Interceptor interceptor,
String jndiName) {
log.debug("Setting new global Hibernate interceptor and restarting.");
Configuration configuration = getConfiguration(jndiName);
configuration.setInterceptor(interceptor);
rebuildSessionFactory();
}

public static Interceptor getInterceptor(String jndiName) {
Configuration configuration = getConfiguration(jndiName);
return configuration.getInterceptor();
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.