Below are code snippets for HibernateUtil and HiberateProcessor that I'm using.
My problem is hibernate issues update statements even though I dont do a transaction commit. I saw this happening when
- A session is first opened and retrieves records list from table A
- Loops thro the list and calls getters on value objects in that list.
- Then without closing the session, a query is made to table B
At this point of time, hibernate issues update statement on all the rows retrieved from table A.
I'm using <sql-query/> to execute a SQL stmt. Plz let me know is there a property in hibernate config which can be used to restrict Hibernate from doing issuing update statements?
Code:
HibernateUtil -
public static Session currentSession(SessionFactory sessionFactory)
{
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null)
{
s = sessionFactory.openSession();
session.set(s);
}
else
{
if (!s.isConnected())
{
closeSession();
s = sessionFactory.openSession();
session.set(s);
}
}
if(!s.isConnected())
s.reconnect();
return s;
}
HibernateProcessor -
public List select(String id, Map data)
{
Session session = HibernateUtil.currentSession(sessionFactory);
Transaction tx = session.beginTransaction();
List results = null;
try
{
Query qry = session.getNamedQuery(id);
String params[] = qry.getNamedParameters();
if(params!=null)
{
for(int i=0; i<params.length; i++)
{
Object paramVal = data.get(params[i]);
qry.setParameter(params[i], paramVal);
}
}
results = qry.list();
}catch(Exception e)
{
if(e instanceof JDBCConnectionException)
{
boolean flag = handleJDBCConnectionException((JDBCConnectionException) e, session, data);
if(flag)
return null;
select(id, data);
}
else
logger.error(ExceptionUtils.getFullStackTrace(e));
}finally
{
try
{
if(tx!=null)
if(tx.isActive())
tx.rollback();
}catch(Exception e)
{}
}
return results;
}