-->
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.  [ 6 posts ] 
Author Message
 Post subject: hibernate JTA configuration
PostPosted: Thu Jan 31, 2008 6:03 am 
Newbie

Joined: Mon Jan 28, 2008 1:55 am
Posts: 8
hi all
i was spending lot of time with configuring hbm.xml file to use JTA transactions.
still i couldn't fix it. Can any one send me a simple working example of how configure and use JTA transaction.

thanks
wijitha....


Top
 Profile  
 
 Post subject: Re: hibernate JTA configuration
PostPosted: Thu Jan 31, 2008 11:34 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Are you using BMT or CMT? In case of BMT you will have to set the following properties:

Code:
hibernate.transaction.factory_class = \
    org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
    org.hibernate.transaction.JBossTransactionManagerLookup


and if CMT is used:

Code:
hibernate.transaction.factory_class = \
    org.hibernate.transaction.CMTTransactionFactory
hibernate.transaction.manager_lookup_class = \
    org.hibernate.transaction.JBossTransactionManagerLookup


The transaction manager lookup class is dependent on what app server you use.



Farzad-


Top
 Profile  
 
 Post subject: Re: hibernate JTA configuration
PostPosted: Thu Jan 31, 2008 11:59 pm 
Newbie

Joined: Mon Jan 28, 2008 1:55 am
Posts: 8
hi
I'm using BMT transaction. i changed my hibernate.hbm.xml file according to your reply bt now i'm getting this error.

hibernate,hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bank_ex</property>
<property name="connection.username">root</property>
<property name="connection.password">wijithaw</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.current_session_context_class">jta</property>

<property name="hbm2ddl.auto">create</property>

<mapping resource="dev/Account.hbm.xml"/>
<mapping resource="dev/Customer.hbm.xml"/>

</session-factory>


java code

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Account account = (Account) session.load(Account.class, accountNum);
account.withdraw(amount);
tx.commit();
} catch (RuntimeException e) {
//To change body of catch statement use File | Settings | File Templates.
tx.rollback();
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error Message :" + e.getMessage() + ".\n");
} finally {
session.close();
}

Hibernate.Util class

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xm
sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("***********Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {

return sessionFactory;
}

}



error message

[java] Feb 1, 2008 9:15:22 AM org.hibernate.util.NamingHelper getInitialContext
[java] INFO: JNDI InitialContext properties:{}
[java] ***********Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not locate TransactionManager
[java] Exception in thread "main" java.lang.ExceptionInInitializerError
[java] at util.HibernateUtil.<clinit>(Unknown Source)
[java] at dev.BankManager.storeCustomer(Unknown Source)
[java] at dev.BankManager.createCustomer(Unknown Source)
[java] at dev.BankManager.addAccount(Unknown Source)
[java] at dev.BankRannable.run(Unknown Source)
[java] at dev.BankManager.main(Unknown Source)
[java] Caused by: org.hibernate.HibernateException: Could not locate TransactionManager
[java] at org.hibernate.transaction.JNDITransactionManagerLookup.getTransactionManager(JNDITransactionManagerLookup.java:26)
[java] at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:257)
[java] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1054)
[java] ... 6 more
[java] Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
[java] at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
[java] at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
[java] at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
[java] at javax.naming.InitialContext.lookup(InitialContext.java:392)
[java] at org.hibernate.transaction.JNDITransactionManagerLookup.getTransactionManager(JNDITransactionManagerLookup.java:23)
[java] ... 8 more
[java] Java Result: 1

BUILD SUCCESSFUL

can u figure out the mis take i have done.

thanks
wijitha...


Top
 Profile  
 
 Post subject: Re: hibernate JTA configuration
PostPosted: Fri Feb 01, 2008 12:14 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Where are you running this? Inside JBoss?

Farzad-


Top
 Profile  
 
 Post subject: Re: hibernate JTA configuration
PostPosted: Wed Feb 06, 2008 12:51 am 
Newbie

Joined: Mon Jan 28, 2008 1:55 am
Posts: 8
farzad wrote:
Where are you running this? Inside JBoss?

Farzad-


i think no. How do i run this inside JBoss.

wijitha


Top
 Profile  
 
 Post subject: Re: hibernate JTA configuration
PostPosted: Wed Feb 06, 2008 10:56 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
wijitha wrote:
farzad wrote:
Where are you running this? Inside JBoss?

Farzad-


i think no. How do i run this inside JBoss.

wijitha


You will need to package either a Java enterprise application or a java web application so that you can deploy in a application server like JBoss. In order to use a transaction manager you will also need to be in an environment that has a transaction manager. From your code I guess this is a standalone application and in a standalone application these two services are not present unless configured to be so.



Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.