JNDI initial context for JtaTransactionManager using Spring and Glassfish
Asked Answered
M

0

6

I want to use JTA Transaction Manager for my application. I'm using Spring and Glassfish. I want to use JNDI lookup to get the JtaTransactionManager instance. My Spring bean configuration for JtaTransactionManager is set up as follows:

<tx:annotation-driven transaction-manager="jtaTransactionManager"/>

<bean id="jtaTransactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManagerName" value="java:comp/TransactionManager"/>
    <property name="userTransactionName" value="java:comp/UserTransaction"/>
</bean>

But when I run the program, it reported errors:

Caused by: org.springframework.transaction.TransactionSystemException: JTA UserTransaction is not available at JNDI location [java:comp/UserTransaction]; nested exception is 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

which is associated with Non-initialized JNDI context. So I added a resource file jndi.properties which adds the missing initial context reported above:

java.naming.factory.initial=???

But the question is: What should I put in the above JNDI initial context field to be able get Glassfish transaction manager via JNDI lookup? I searched a number of sources but didn't find this piece of information. The official document only says:

To access a UserTransaction instance, you can either look it up using the java:comp/UserTransaction JNDI name or inject it using the @Resource annotation.

So how should I set up the JNDI initial factory for JTA Transaction Manager in jndi.properties file so that both TransactionManager and UserTransaction can be injected into my jtaTransactionManager bean by JNDI lookup? Thanks.

Monarchal answered 16/3, 2014 at 19:53 Comment(6)
Instead of using the JtaTransactionManager use <tx:jta-transaction-manager /> that does detection on which server you are running and does some environment specific lookups and configuration.Chervil
@M.Deinum I tried this but doesn't work. It says No JTA UserTransaction available - specify either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName'.Monarchal
If you are running the application on/in glassfish it should work, it is what we use here in production for a couple of applications. Which glassfish and spring version are you using?Chervil
Glassfish version is 3.1 and Spring version is 3.2. So I don't need to specify the JNDI initial context in jndi.properties file in order to detect the transaction manager?Monarchal
Never added that file only <tx:jta-transaction-manager /> when using a local transactionmanager. If you are using a remote JNDI you will have to add that file. But I assume you are deploying on the same glassfish as you want to use for transactions.Chervil
Yes I'm deploying it on the same server.Monarchal

© 2022 - 2024 — McMap. All rights reserved.