batmat wrote:
Do you run in an appserver? If so, maybe have a look at the session management, so as to see for example how it's configured and if the threads are not sharing the same session...
Yah, I am running it within an appserver.
The follow is the configuration of the SessionFactory which defined in the hibernate-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server>
<server>
<mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=ISAT-Hibernate">
<attribute name="DatasourceName">java:/myDS</attribute>
<attribute name="Dialect">org.hibernate.dialect.PostgreSQLDialect</attribute>
<attribute name="SessionFactoryName">
java:/hibernate/TrailSessionFactory
</attribute>
<attribute name="DefaultSchema">trail</attribute>
<attribute name="ShowSqlEnabled">true</attribute>
<attribute name="Username">trail</attribute>
<attribute name="Password">trail</attribute>
</mbean>
</server>
The way I obtain the SessionFactory
Code:
public class HibernateUtil
{
//the name is the jndi name as define in the above config file
public static SessionFactory getSessionFactory(String name) throws NamingException
{
JndiFinder finder = new JndiFinder(null);
return (SessionFactory)finder.lookup(name, SessionFactory.class);
}
}
The way i obtain the CurrentSession
Code:
public void beginTransaction()
{
_currTx = getCurrentSession().beginTransaction();
}
public Session getCurrentSession()
{
//the sessionFactory is obtained through the HibernateUtil
return _sessionFactory.getCurrentSession();
}
I have compared the hashcode of the currentSession that obtained by the two MDBean, and they are different.
The following is the debug msg generated by the Hibernate.
//Note: the first MDBean instance
[03:01:07-19:15:13,645] [DEBUG:org.hibernate.jdbc.AbstractBatcher] [JMS SessionPool Worker-106] about to close PreparedStatement (open PreparedStatements: 1, globally: 38)
[03:01:07-19:15:13,645] [DEBUG:org.hibernate.jdbc.AbstractBatcher] [JMS SessionPool Worker-106] closing statement
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.jdbc.AbstractBatcher] [JMS SessionPool Worker-106] closing JDBC connection (open PreparedStatements: 0, globally: 37) (open ResultSets: 0, globally: 0)
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.event.def.AbstractFlushingEventListener] [JMS SessionPool Worker-106] post flush
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.transaction.JTATransaction] [JMS SessionPool Worker-106] commit
//The first MDBean instance finsihed invoking the handler
[03:01:07-19:15:13,661] [ INFO:STDOUT] [JMS SessionPool Worker-106] 19:15:13,661 INFO [ISAT] [TrailInfoHandler.processTrailInfo()] End Process TrailInfo
[03:01:07-19:15:13,661] [ INFO:STDOUT] [JMS SessionPool Worker-106] End Process TrailInfo
//Second MDBean start accessing the handler, the first transaction seem like not yet completed.
[03:01:07-19:15:13,661] [ INFO:STDOUT] [JMS SessionPool Worker-107] 19:15:13,661 INFO [ISAT] [TrailInfoHandler.processTrailInfo()] Start processing TrailInfo ]
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.transaction.CacheSynchronization] [JMS SessionPool Worker-106] transaction before completion callback
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.transaction.CacheSynchronization] [JMS SessionPool Worker-106] automatically flushing session
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.impl.SessionImpl] [JMS SessionPool Worker-106] automatically flushing session
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.jdbc.JDBCContext] [JMS SessionPool Worker-106] before transaction completion
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.impl.SessionImpl] [JMS SessionPool Worker-106] before transaction completion
-----
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.transaction.JTATransaction] [JMS SessionPool Worker-107] begin
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.transaction.JTATransaction] [JMS SessionPool Worker-107] Looking for UserTransaction under: UserTransaction
[03:01:07-19:15:13,661] [DEBUG:org.hibernate.transaction.JTATransaction] [JMS SessionPool Worker-107] Obtained UserTransaction
//
The second MDBean instance go and retrieve the commonInfo, but it can't find it
-----
[03:01:07-19:15:13,707] [DEBUG:org.hibernate.transaction.CacheSynchronization] [JMS SessionPool Worker-106] transaction after completion callback, status: 3
[03:01:07-19:15:13,707] [DEBUG:org.hibernate.jdbc.JDBCContext] [JMS SessionPool Worker-106] after transaction completion
[03:01:07-19:15:13,707] [DEBUG:org.hibernate.impl.SessionImpl] [JMS SessionPool Worker-106] after transaction completion
[03:01:07-19:15:13,707] [DEBUG:org.hibernate.transaction.CacheSynchronization] [JMS SessionPool Worker-106] automatically closing session
[03:01:07-19:15:13,707] [DEBUG:org.hibernate.impl.SessionImpl] [JMS SessionPool Worker-106] automatically closing session
[03:01:07-19:15:13,707] [DEBUG:org.hibernate.impl.SessionImpl] [JMS SessionPool Worker-106] closing session
Is it true that after the executed the above method, the changes made by the first MDBean instance will be written to the DB ?
How we ensure that the first transaction is completed prior we start the second transaction?
Many Thanks