-->
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.  [ 3 posts ] 
Author Message
 Post subject: CMT Txn issue with an application running in weblogic
PostPosted: Tue Sep 15, 2009 5:19 pm 
Newbie

Joined: Wed Sep 12, 2007 4:13 pm
Posts: 4
Location: Atlanta
Hello,

I am trying to set up an simple application which uses CMT as well as hibernate as persitsance framework. The application is deployed in Weblogic 9.2 app server.

Please see my hibernate.cfg.xml:
Code:
<property name="hibernate.connection.datasource">ssm_oracle</property>
      <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
      <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
      <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
      <property name="hibernate.current_session_context_class">jta</property>

      <property name="hibernate.connection.release_mode">auto</property>
      
      <property name="hibernate.transaction.auto_close_session">true</property>
      
      
      <mapping resource="com/ssm/business/Employ.hbm.xml" />



The following is a method present in a Stateless Session bean using CMT:

Code:
   public void saveEmploy(){
      Session session = HibernateUtil.getCurrentSession();
      
      com.ssm.business.Employ emp = new com.ssm.business.Employ();
      emp.setName("Employ 46");
      emp.setDepartment(10);
      session.save(emp);

   }



After executing this code, nothing is getting inserted into the database. If I explicitly begin transaction and commit using Hibernate API, it gets insert.

Can anyone suggests what I am missing here?

Thanks
Shimit


Last edited by shimitsm on Wed Sep 16, 2009 1:28 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: CMT Txn issue with an application running in weblogic
PostPosted: Wed Sep 16, 2009 10:07 am 
Newbie

Joined: Wed Sep 12, 2007 4:13 pm
Posts: 4
Location: Atlanta
Do I need to modify my java code as shown following to do the commit:

Code:
  public void saveEmploy(){
      Session session = HibernateUtil.getCurrentSession();
     
      com.ssm.business.Employ emp = new com.ssm.business.Employ();
      emp.setName("Employ 46");
      emp.setDepartment(10);
      session.save(emp);
      session.flush();
      session.close();
   }


Here I added session.flush(); and session.close();. Are these lines necessary to do the database insert in a CMT managed bean?

Thanks
Shimit

_________________
SSM


Last edited by shimitsm on Wed Sep 16, 2009 1:29 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: CMT Txn issue with an application running in weblogic
PostPosted: Wed Sep 16, 2009 1:08 pm 
Newbie

Joined: Wed Sep 12, 2007 4:13 pm
Posts: 4
Location: Atlanta
I found the answer to the problem. I am sure I am not the first person experiencing this issue, but I didn't find a satisafactory explanation to this issue in any of the forums. So I am going to put as much detail to help whoever encounter the same issue.

Key here is the following properties in the hibernate config file:
Code:
<!--
This need to be mentioned in order to automatically bind the sessionfactory to the JNDI tree. Here in this case the JNDI name would be "oracle_session_factory". If we omit this property hibernate will not bind the sessionfactory to JNDI.

In the hibernate util class first look up sessionfactory using the following code:

SessionFactory factory = new InitialContext().lookup("oracle_session_factory");


If the above statement throws NamingException or return null value, do the following:


Configuration config = new Configuration().configure( "hibernate_config_file");
config.buildSessionFactory();

Please note, we need to call config.buildSessionFactory() to register the factory to the JNDI tree.

-->
<property name="session_factory_name">oracle_session_factory</property>


<--
Mention your datasource's JNDI name here
-->
      <property name="connection.datasource">ssm_oracle</property>
      <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
      <property name="show_sql">true</property>
<--
Since we are using CMT make sure the config contains the follwing value:
-->
      <property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
      
      <property name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
      <property name="current_session_context_class">org.hibernate.context.JTASessionContext</property>

      <property name="connection.release_mode">auto</property>
<--
The following two entries are essential to do the automatic flush into the database at the end of the transaction. By specifying these, we do not need to explicitly flush and close the session, it will be automatically done at the end of the transaction. I did not specify these earlier, that was the reason the record was not getting inserted into DB.
-->      
      <property name="transaction.auto_close_session">true</property>
      <property name="transaction.flush_before_completion">true</property>


By adding the above changes in the config files, we do not need to put any transaction begin, close, session flush, session close code in our source code. A working sample is given below:

Code:
   public void saveEmploy(){
      Session session = HibernateUtil.getCurrentSession();     
      com.ssm.business.Employ emp = new com.ssm.business.Employ();
      emp.setName("Employ 46");
      emp.setDepartment(10);
      session.save(emp);
   }


If anything is not clear, let me know, I shall try to put more detail.

Thanks
Shimit

_________________
SSM


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

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.