Question is very similar to what I asked earlier:
https://stackoverflow.com/questions/27549538/hibernate-spring-weblogic-and-transaction-management#comment43532319_27549538
Transaction Management using Hibernate with weblogic
Using Oracle db , weblogic app server , datasource configured in weblogic server
Creating SessionFactories in code rather than by using application context and bean definition
@m-deinum - thanks for your inputs and yes sorry - I know you have been questioning me on why am doing it this way and that I should be looking at multi tenant support - I will - but call me pig-headed - just wanted to get this closed :)
So I got this to work - albeit - not a clean solution
Please help me understand why it works and why something that should work is throwing exceptions!
Hibernate.cfg.xml: ( which contains weblogic datasource )
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">MYDS</property>
<property name="show_sql">true</property>
<property name="default_schema">CLIENT1SCHEMA</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
NOTE - have used 'thread' in 'current_session_context_class'
Code that works:
public List<T> findAll(final HashMap param) {
List<T> listData = null;
Criteria subSelectCriteria =null;
try {
Session session=getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
subSelectCriteria =session.createCriteria(getType());
} catch (HibernateException e) {
e.printStackTrace();
}
boolean isDistinctQuery=false;
subSelectCriteria.setFirstResult(0);
subSelectCriteria.setMaxResults(100);
List inObjList = subSelectCriteria.list();
if(!inObjList.isEmpty()){
listData = inObjList;
}
/** one of the below needs to be commented for this to work !!! - which makes no sense ? **/
tx.commit();
session.close();
return listData;
}
If I commit and then close the session am getting a weird error :
org.hibernate.SessionException: Session was already closed
Now - if I comment out either tx.commit OR session.close code works fine !
why ?
is this ok ?
Thanks
@m-deinum - will definitely start exploring the Multitenant approach and I actually spent a day - but I did not get an end to end implementation / example I could follow