My original post got obsolete after my latest findings , which is why i am posting a new question.
I am trying to use JTA programmatically using JTA's UserTransaction interface + Hibernate Core API. My Hibernate version is 3.2.4 SP1. Using JBoss 421GA Application Server.
Following the suggestion provided at
http://www.hibernate.org/42.html#A5
[[ I am also following Hibernate 3.2 persistence book , Chapter 10, Transactions & Concurrency, page 442
Programmatic Transactions with JTA]]
Main Question:
A.
The above article suggests to lookup java:/UserTransaction in the businessObject. I have seen another suggestion else where to register the
Code:
<property name="jta.UserTransaction">java:/UserTransaction</property>
in Hibernate.cfg.xml. So I did both. But when running my unit-test I get "could not locate TransactionManager" exception message.
My question is why is hibernate class
org.hibernate.transaction.JBossTransactionManagerLookUp looking for java:/TransactionManager instead of
java:/UserTransaction which is what the Hibernate.cfg.xml and BusinessObject is suggested to be looked up.
If I figure this out, I think I can fix the "could not locate Transaction
Manager" exception
B. this post on jboss forum
http://www.jboss.com/?module=bb&op=viewtopic&t=35448suggests to look up java:/UserTransaction when running outside of jboss VM
Quote:
it's not possible to get the TransactionManager outside the jboss VM (i.e. from the client VM). To manage a transaction from the client side use java:/UserTransaction instead.
In my case, I am running a standalone junit test on my businessObject before hooking it up into my
Struts based web-app. While running this unit-test (vs running struts web-app) when should i use java:/UserTransaction vs
java:/TransactionManager
Details on my findings and my code snippets/logs/exception/hibernate config file:My BusinessObject is looking up the UserTransaction in its constructor as
Code:
UserTransaction utx=null;
Context ctx=null;
public PatientManager() throws Exception{
Configuration conf = new Configuration();
conf.setProperties(getInitialContextProperties());
utx= (UserTransaction) new InitialContext().lookup("java:/UserTransaction");
}
public static InitialContext getInitialContext() throws Exception {
Hashtable props = getInitialContextProperties();
return new InitialContext(props);
}
private static Properties getInitialContextProperties() {
Properties props = new Properties();
props.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
props.put("java.naming.provider.url","jnp://localhost:1099");
return props;
}
But when its businessMethod is called, which uses the classic (HibernateUtil ) singleton pattern to get the sessionFactory
as shown below...
Code:
public Patient getNextPatient(Integer ptID) throws Exception {
Patient tmpPatient = new Patient();
Session session = null;
Statement stmt = null;
Patient patient = null;
try {
utx.begin();
session = HibernateUtil.getSessionFactory().getCurrentSession();
.......
...
..
which calls the following HibernateUtil code (a very standard version borrowed from Hibernate 3.2 persistence book)
Code:
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
...........<getters and setters here>
....
..
}
I get an exception when .buildSessionFactory() function is called in the above mentioned static block...
The exception trace points out that this function
JNDITransactionManagerLookup.getTransactionManager()
"Could not locate TransactionManager"
I checked the Hibernate API code for this function call...
it looks like this
Code:
return (TransactionManager) NamingHelper.getInitialContext(props).lookup( getName() );
The getName() function is implemented by JBossTransactionManagerLookup class as following
Code:
protected String getName() {
return "java:/TransactionManager";
}
My question is why is hibernate looking for java:TransactionManager instead of
java:/UserTransaction which is what the Hibernate.cfg.xml and BusinessObject is suggested to lookup.
Code:
java.lang.ExceptionInInitializerError
at com.bcdrg.library.HibernateUtil.<clinit>(HibernateUtil.java:13)
at com.bcdrg.test.PatientTest.tearDown(PatientTest.java:118)
at junit.framework.TestCase.runBare(TestCase.java:143)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:131)
at junit.framework.TestSuite.runTest(TestSuite.java:173)
at junit.framework.TestSuite.run(TestSuite.java:168)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.HibernateException: Could not locate TransactionManager
at org.hibernate.transaction.JNDITransactionManagerLookup.getTransactionManager(JNDITransactionManagerLookup.java:29)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:325)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at com.bcdrg.library.HibernateUtil.<clinit>(HibernateUtil.java:11)
... 14 more
Caused by: javax.naming.NameNotFoundException: TransactionManager not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
at sun.reflect.GeneratedMethodAccessor120.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:466)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:707)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:589)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at org.hibernate.transaction.JNDITransactionManagerLookup.getTransactionManager(JNDITransactionManagerLookup.java:26)
... 17 more
Debug level Hibernate log excerpt:Code:
log4j:ERROR Could not find value for key log4j.appender.SQL_APPENDER
log4j:ERROR Could not instantiate appender named "SQL_APPENDER".
09:46:21,625 INFO PatientTest:66 - @@@printNextPatient(int ptID)-started
09:46:21,687 INFO Environment:514 - Hibernate 3.2.4.sp1
09:46:21,687 INFO Environment:547 - hibernate.properties not found
09:46:21,687 INFO Environment:681 - Bytecode provider name : cglib
09:46:21,703 INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
09:46:22,187 INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
09:46:22,187 INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml
09:46:22,328 INFO Configuration:553 - Reading mappings from resource : hello/Message.hbm.xml
09:46:22,437 INFO HbmBinder:300 - Mapping class: hello.Message -> MESSAGES
09:46:22,515 INFO Configuration:553 - Reading mappings from resource : com/bcdrg/library/Tbldemographics.hbm.xml
09:46:22,546 INFO HbmBinder:300 - Mapping class: com.bcdrg.library.Patient -> tbldemographics
09:46:22,546 INFO Configuration:1541 - Configured SessionFactory: null
09:46:22,562 INFO NamingHelper:26 - JNDI InitialContext properties:{java.naming.provider.url=jnp://localhost:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory}
09:46:22,578 INFO DatasourceConnectionProvider:61 - Using datasource: postgresDS
09:46:22,718 INFO SettingsFactory:89 - RDBMS: PostgreSQL, version: 8.1.4
09:46:22,734 INFO SettingsFactory:90 - JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.1 JDBC2 with NO SSL (build 405)
09:46:22,781 INFO Dialect:152 - Using dialect: org.hibernate.dialect.PostgreSQLDialect
09:46:22,796 INFO TransactionFactoryFactory:34 - Transaction strategy: org.hibernate.transaction.JTATransactionFactory
09:46:22,796 INFO NamingHelper:26 - JNDI InitialContext properties:{java.naming.provider.url=jnp://localhost:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory}
09:46:22,796 INFO TransactionManagerLookupFactory:38 - instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
09:46:22,796 INFO TransactionManagerLookupFactory:42 - instantiated TransactionManagerLookup
09:46:22,796 INFO SettingsFactory:143 - Automatic flush during beforeCompletion(): disabled
09:46:22,796 INFO SettingsFactory:147 - Automatic session close at end of transaction: disabled
09:46:22,796 INFO SettingsFactory:154 - JDBC batch size: 15
09:46:22,796 INFO SettingsFactory:157 - JDBC batch updates for versioned data: disabled
09:46:22,796 INFO SettingsFactory:162 - Scrollable result sets: enabled
09:46:22,796 INFO SettingsFactory:170 - JDBC3 getGeneratedKeys(): disabled
09:46:22,796 INFO SettingsFactory:178 - Connection release mode: auto
09:46:22,796 INFO SettingsFactory:205 - Default batch fetch size: 1
09:46:22,796 INFO SettingsFactory:209 - Generate SQL with comments: disabled
09:46:22,812 INFO SettingsFactory:213 - Order SQL updates by primary key: disabled
09:46:22,812 INFO SettingsFactory:217 - Order SQL inserts for batching: disabled
09:46:22,812 INFO SettingsFactory:386 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
09:46:22,812 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
09:46:22,812 INFO SettingsFactory:225 - Query language substitutions: {}
09:46:22,812 INFO SettingsFactory:230 - JPA-QL strict compliance: disabled
09:46:22,812 INFO SettingsFactory:235 - Second-level cache: enabled
09:46:22,812 INFO SettingsFactory:239 - Query cache: disabled
09:46:22,812 INFO SettingsFactory:373 - Cache provider: org.hibernate.cache.NoCacheProvider
09:46:22,812 INFO SettingsFactory:254 - Optimize cache for minimal puts: disabled
09:46:22,812 INFO SettingsFactory:263 - Structured second-level cache entries: disabled
09:46:22,828 INFO SettingsFactory:283 - Echoing all SQL to stdout
09:46:22,828 INFO SettingsFactory:290 - Statistics: disabled
09:46:22,828 INFO SettingsFactory:294 - Deleted entity synthetic identifier rollback: disabled
09:46:22,828 INFO SettingsFactory:309 - Default entity-mode: pojo
09:46:22,828 INFO SettingsFactory:313 - Named query checking : enabled
09:46:22,859 INFO SessionFactoryImpl:161 - building session factory
09:46:23,140 INFO SessionFactoryObjectFactory:86 - Factory name: java:hibernate/SessionFactory
09:46:23,140 INFO NamingHelper:26 - JNDI InitialContext properties:{java.naming.provider.url=jnp://localhost:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory}
09:46:23,156 INFO SessionFactoryObjectFactory:91 - Bound factory to JNDI name: java:hibernate/SessionFactory
09:46:23,156 WARN SessionFactoryObjectFactory:101 - InitialContext did not implement EventContext
09:46:23,156 INFO NamingHelper:25 - @@@Robin : getName()java:/TransactionManager
09:46:23,156 INFO NamingHelper:26 - JNDI InitialContext properties:{java.naming.provider.url=jnp://localhost:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory}
09:46:23,171 INFO PatientTest:117 - @@@tearDown()-started
09:46:23,171 INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
09:46:23,171 INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml
09:46:23,171 INFO Configuration:553 - Reading mappings from resource : hello/Message.hbm.xml
09:46:23,203 INFO HbmBinder:300 - Mapping class: hello.Message -> MESSAGES
09:46:23,203 INFO Configuration:553 - Reading mappings from resource : com/bcdrg/library/Tbldemographics.hbm.xml
09:46:23,218 INFO HbmBinder:300 - Mapping class: com.bcdrg.library.Patient -> tbldemographics
09:46:23,218 INFO Configuration:1541 - Configured SessionFactory: null
09:46:23,218 INFO NamingHelper:26 - JNDI InitialContext properties:{java.naming.provider.url=jnp://localhost:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory}
09:46:23,234 INFO DatasourceConnectionProvider:61 - Using datasource: postgresDS
09:46:23,281 INFO SettingsFactory:89 - RDBMS: PostgreSQL, version: 8.1.4
09:46:23,296 INFO SettingsFactory:90 - JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.1 JDBC2 with NO SSL (build 405)
09:46:23,312 INFO Dialect:152 - Using dialect: org.hibernate.dialect.PostgreSQLDialect
09:46:23,312 INFO TransactionFactoryFactory:34 - Transaction strategy: org.hibernate.transaction.JTATransactionFactory
09:46:23,312 INFO NamingHelper:26 - JNDI InitialContext properties:{java.naming.provider.url=jnp://localhost:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory}
09:46:23,312 INFO TransactionManagerLookupFactory:38 - instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
09:46:23,312 INFO TransactionManagerLookupFactory:42 - instantiated TransactionManagerLookup
09:46:23,312 INFO SettingsFactory:143 - Automatic flush during beforeCompletion(): disabled
09:46:23,312 INFO SettingsFactory:147 - Automatic session close at end of transaction: disabled
09:46:23,312 INFO SettingsFactory:154 - JDBC batch size: 15
09:46:23,312 INFO SettingsFactory:157 - JDBC batch updates for versioned data: disabled
09:46:23,312 INFO SettingsFactory:162 - Scrollable result sets: enabled
09:46:23,312 INFO SettingsFactory:170 - JDBC3 getGeneratedKeys(): disabled
09:46:23,312 INFO SettingsFactory:178 - Connection release mode: auto
09:46:23,312 INFO SettingsFactory:205 - Default batch fetch size: 1
09:46:23,312 INFO SettingsFactory:209 - Generate SQL with comments: disabled
09:46:23,312 INFO SettingsFactory:213 - Order SQL updates by primary key: disabled
09:46:23,312 INFO SettingsFactory:217 - Order SQL inserts for batching: disabled
09:46:23,312 INFO SettingsFactory:386 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
09:46:23,312 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
09:46:23,312 INFO SettingsFactory:225 - Query language substitutions: {}
09:46:23,312 INFO SettingsFactory:230 - JPA-QL strict compliance: disabled
09:46:23,312 INFO SettingsFactory:235 - Second-level cache: enabled
09:46:23,312 INFO SettingsFactory:239 - Query cache: disabled
09:46:23,328 INFO SettingsFactory:373 - Cache provider: org.hibernate.cache.NoCacheProvider
09:46:23,328 INFO SettingsFactory:254 - Optimize cache for minimal puts: disabled
09:46:23,328 INFO SettingsFactory:263 - Structured second-level cache entries: disabled
09:46:23,328 INFO SettingsFactory:283 - Echoing all SQL to stdout
09:46:23,328 INFO SettingsFactory:290 - Statistics: disabled
09:46:23,328 INFO SettingsFactory:294 - Deleted entity synthetic identifier rollback: disabled
09:46:23,328 INFO SettingsFactory:309 - Default entity-mode: pojo
09:46:23,328 INFO SettingsFactory:313 - Named query checking : enabled
09:46:23,328 INFO SessionFactoryImpl:161 - building session factory
09:46:23,343 INFO SessionFactoryObjectFactory:86 - Factory name: java:hibernate/SessionFactory
09:46:23,343 INFO NamingHelper:26 - JNDI InitialContext properties:{java.naming.provider.url=jnp://localhost:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory}
09:46:23,343 INFO SessionFactoryObjectFactory:91 - Bound factory to JNDI name: java:hibernate/SessionFactory
09:46:23,343 WARN SessionFactoryObjectFactory:101 - InitialContext did not implement EventContext
09:46:23,343 INFO NamingHelper:25 - @@@Robin : getName()java:/TransactionManager
09:46:23,359 INFO NamingHelper:26 - JNDI InitialContext properties:{java.naming.provider.url=jnp://localhost:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory}
Here's my Hibernate.cfg.xmlCode:
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">
postgresDS
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<property name="hibernate.session_factory_name">
java:hibernate/SessionFactory
</property>
<!-- Show and print nice SQL on stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- JTA stuff -->
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.jndi.class">org.jnp.interfaces.NamingContextFactory</property>
<property name="hibernate.jndi.url">jnp://localhost:1099</property>
<property name="jta.UserTransaction">java:/UserTransaction</property>
<!-- List of XML mapping files -->
<mapping resource="hello/Message.hbm.xml" />
<mapping resource="com/bcdrg/library/Tbldemographics.hbm.xml" />
</session-factory>
</hibernate-configuration>