-->
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 with hibernate session
PostPosted: Thu Sep 08, 2005 11:47 am 
Newbie

Joined: Wed Sep 07, 2005 10:23 am
Posts: 4
How to make hibernate use existing Container Managed Transaction.

Following is the code of my method which uses hibernate to save data. This is method in AccountSessionBean which uses Container Managed Transaction.

public void insertAccount() throws Exception{
Session session = null;
Transaction txn = null;

try{

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
Account account = new Account();
account.setAccountID("2");
account.setAccountName("ADM");
account.setOpeningDate(new Date());
account.setClosingDate(new Date());
session.save(account);
}catch(Exception e){
e.printStackTrace();
}finally{
session.flush();
session.close();
}
}


The above method is invoked from another method in a Session Bean called TMSessionBean which is also a CMT. I want both the methods to part of single Transaction.

public void updateTables() throws Exception
{

AccountSessionRemote objAccountSessionRemote = null;
try{
objAccountSessionRemote = (AccountSessionRemote)getRemote("ejb/AccountSessionBean");
insertTable1(); // This method inserts data into table using normal JDBC calls
objAccountSessionRemote.insertAccount();
selectTable1();

}catch(Exception e)
{
e.printStackTrace();
if(utx != null)
{
System.out.println("UTX IS NOT NULL");
utx.rollback();
}
System.out.println("Exception in updateTables");
throw new EJBException();
}
}


When i execute the code no error is thrown. Everything runs fine without any exceptions.

When i try to query on the tables, the table which is updated by the AccountSessionBean is locked. The other table is inserted.

1. Can anyone suggest what iam doing wrong.
2. What should i do to use the existing CMT.


Top
 Profile  
 
 Post subject: Re: CMT with hibernate session
PostPosted: Tue Mar 04, 2014 4:29 am 
Newbie

Joined: Tue Mar 04, 2014 12:57 am
Posts: 2
Hi,

We are facing a simlar problem with our application. Currenty we are using EJB2 with weblogic 10.3 as our app server. For a new module we are trying to use Hibernate, but when tried to use CMT we are not able to save data in our application using hibernate. Basically we are not able to run our hibernate code in the transaction provided by container.

if i use :
Transaction tx = session.beginTransaction();
.
.
tx.commit();
then it works fine.

my CFG :

<property name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
<property name="transaction.factory.class">org.hibernate.transaction.CMTTransactionFactory</property >
<property name ="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="current_session_context_class">jta</property>

<property name="hibernate.connection.release_mode">auto</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.session_factory_name">HibernateSessionFactory</property>
<property name="hibernate.jndi.weblogic.jndi.replicateBindings">false</property>
----------------------------------------------

Anybody having a solution to this please reply.

Regards,
Dacs_Sach


Top
 Profile  
 
 Post subject: Re: CMT with hibernate session
PostPosted: Wed Mar 05, 2014 5:51 am 
Newbie

Joined: Tue Mar 04, 2014 12:57 am
Posts: 2
Hi Guys,

Got a solution to the above problem, so posting it.

Some modules in my application will use Entity bean and some will use Hibernate to communicate to peristant store.
I am using CMT(Weblogic) and the task was to get this both working in a single transaction. Approach we used is :

1> Created a war and put it in our Application EAR which contains entities and other jar files.
2> Used ServletContextListener to initiate a class MySessionfactory.
3> Use this session factory in my CMT to get current session.
4> Now in a single transaction I can save entity beans as well as hibernate objects.

My CFG :

<!-- ****************************Datasource******************************** -->
<property name="connection.username">weblogic</property>
<property name="connection.password">weblogic</property> <!— weblogic console username and password -- >
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.datasource">myPool</property>
<property name="hibernate.use_sql_comments">true</property>

<!-- Transaction settings -->
<property name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
<property name="transaction.factory.class">org.hibernate.transaction.CMTTransactionFactory</property >
<property name ="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="current_session_context_class">jta</property>
<property name="show_sql">true</property>

<mapping resource="Emp.hbm.xml"/>

-------------------------------------------------------------------------------------------
MyServletContextListener code :

private static final SessionFactory sessionFactory;

protected Session session = null;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("sessionFactory--->>>"+sessionFactory);

} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
---------------------------------------------------------------------------------------------------------------
In a CMT :

Method:
//Some other entity beans fetch and save code
.
.
.
SessionFactory sessionFactory = MyServletContextListener.getSessionFactory();
Session session = null;
try {
session = sessionFactory.getCurrentSession();
} catch (Exception he) {
he.printStackTrace();
session = sessionFactory.openSession();
}
Emp emp = new Emp ("DSS","Sach",new Date(),77.77);
session.save(cd);
session.flush();
--------------------------------------------------------------------------------------------
Here important thing is to use session.flush(), only then ur hibernate code will get committed. Don’t use session.close() here else the hibernate objects will not get persisted.

The above setting are working fine with : EJB2 and weblogic 10.3 and hibernate 4. Have a good time 

Regards,
Dacs_sach(Kolte S)


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.