0

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

Community
  • 1
  • 1
satish marathe
  • 1,073
  • 5
  • 18
  • 37
  • @m-deinum - sorry to bug you - but your inputs are welcome :) – satish marathe Dec 22 '14 at 14:56
  • I'm going to point you to the multi tenancy support again :) sorry. But instead of putting your efforts in getting your, sorry for the word, contraption to work you are probably better of (and simpler) using the default multi tenancy support. But to help you with that we would have to know how you have implemented this multi tenancy stuff, is it schema based or database/datasource based. – M. Deinum Dec 22 '14 at 15:08
  • @m-deinum - sure - no problem - it is schema based - so same session factories - the only thing that would differ is the schema - and thanks for your patience - I will start working in earnest on the multi tenancy :) – satish marathe Dec 22 '14 at 16:45

0 Answers0