How can I get UserTransaction from a EJB Client?

Hi

Anyone can tell me what is the correct way to get the UserTransaction from a EJB Client? I've tried the following code in JBoss, but it gives "javax.naming.NameNotFoundException: comp not bound" error:

Properties env = new Properties();

env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");

env.setProperty("java.naming.provider.url", "localhost:1099");

Context ctx = new InitialContext(env);

UserTransaction ut = (UserTransaction)ctx.lookup("java:comp/UserTransaction");

I've read through this news group and found quite a number of people is facing the same problem. Are there any experts to give some explanation?

Also someone has said that it is not a good practice to manage the transaction at the EJB client level. But it is quite common that my client needs to involves EJB1.operation1() and EJB2.operation2(), and I want to put them into a transaction. So what is the correct way to handle this situation?

Best Regards,

Rongan

[1051 byte] By [rongan] at [2007-9-26 2:26:05]
# 1

Hi,

I am not sure about EJBoss in general, but apparently client-managed transaction management is not supported in you r case.

As to how to handle your example situation: you would need to set up a new server (and beans) that call these two operations as part of a new service ( read: bean ).

Of course, the new server would have to be able to interoperate with EJBoss servers regarding transaction termination...

All the best,

Guy

GuyPardon at 2007-6-29 9:38:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
drop the comp from the original name (ie. lookup("java:UserTransaction"))
sweetfa at 2007-6-29 9:38:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
try ejbContext.getUserTransaction().Cheers,John...
john_arun at 2007-6-29 9:38:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
use rpc,and set transcation control in the bean method!.client call the remote method and control the transcation
yellowicq at 2007-6-29 9:38:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
just useUserTransaction ut = (UserTransaction)ctx.lookup("UserTransaction");
koonkay at 2007-6-29 9:38:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

I do not know that you can do this, because in a distributed environment

you could have multiple ejb servers, each will have a UserTransaction

object bound to their naming space. In a situation where a ejb in one app server call another ejb on a different app server. Looking for UserTransaction in this case does not work.

However if you have talking about a stand alone server,

you should able to do a look.

There is no naming convention for UserTransaction in the JTA spec, different App server have their own way of naming the UserTransaction.

I suggest you call list on the initialcontext object. This will list all of the objects bound to the naming space.

Having saying all that if the aim of the above exercise is to enable to run EJB1.operation1() and EJB2.operation2() in the same transaction. It is a wrong way to go about. The easiest and standard way is define EJB1 and EJB2 as contain manage transaction and the methods(operation1() and operation2()) transaction attribute is Required.

jacktruong at 2007-6-29 9:38:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

For what it's worth, I just got this to work for JBoss 4.0.x.

I have a (stateless) session bean that requires transactions. I was trying to write a JUnit test to test transaction commit and rollback.

I finally got it to work with code like this in the JUnit test:

Hashtable props = new Hashtable();

props.put(

Context.INITIAL_CONTEXT_FACTORY,

"org.jnp.interfaces.NamingContextFactory"

);

props.put(

Context.URL_PKG_PREFIXES,

"org.jboss.naming:org.jnp.interfaces"

);

props.put(

Context.PROVIDER_URL,

"jnp://localhost:1099"

);

Context ctx = new InitialContext(props);

Object ref = ctx.lookup("UserTransaction");

UserTransaction transaction = (UserTransaction)PortableRemoteObject.narrow(

ref, UserTransaction.class

);

So far this looks normal, and it is. The trick was in specifying the IntialContext properties in the environment of the JUnit VM _as well_. I was using eclipse, so I specified the following in the Run ... dialog for the unit test:

-Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

-Djava.naming.provider.url=jnp://localhost:1099

-Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Before adding the '-D' system properties, I was getting strange exceptions on the first call to transaction.begin().

Aaron

Aaron.Aston at 2007-6-29 9:38:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...