Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

lookup on UserTransaction  RSS feed

 
Christophe Grosjean
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Is it possible to perform a lookup to get a UserTransaction outside an EJB, i.e. from a client thread running in the server ?
When i do this :
...
ctx = new InitialContext();
ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
...
I get :
javax.naming.NoInitialContextException: Cannot create context for 'null' outside the scope of an application.
at weblogic.jndi.factories.java.javaURLContextFactory.getObjectInstance(javaURLContextFactory.java:50)
at javax.naming.spi.NamingManager.getURLObject(NamingManager.java:577)
at javax.naming.spi.NamingManager.getURLContext(NamingManager.java:526)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:273)
at javax.naming.InitialContext.lookup(InitialContext.java:345)
at com.puilaetco.mpi.fwk.server.business.synchro2.MPIMessageBroker.run(MPIMessageBroker.java:84)
at java.lang.Thread.run(Thread.java:479)
Thanks for any help...
Christophe
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ctx = new InitialContext();

What about the Properties Object?
Properties p = new Properties ();
p.put( Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory" );
p.put( Context.PROVIDER_URL,
"t3://localhost:7001" );
ctx = new InitialContext( p );
I copied this code out of OReilly's EJB 3rd ED
page 103.
The following example comes from the J2EE tutorial
public void withdrawCash(double amount) {

UserTransaction ut = context.getUserTransaction();

try {
ut.begin();
updateChecking(amount);
machineBalance -= amount;
insertMachine(machineBalance);
ut.commit();
} catch (Exception ex) {
try {
ut.rollback();
} catch (SystemException syex) {
throw new EJBException
("Rollback failed: " + syex.getMessage());
}
throw new EJBException
("Transaction failed: " + ex.getMessage());
}
}
HTH
 
Christophe Grosjean
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Properties object are just like you wrote. But the lookup is made from a thread running on the weblogic server and not from an EJB.
Christophe
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the thread in the same JVM as the EJBs run in?
JNDI is a service an EJB container is required to provide. In an external JVM more setup will be required.
 
Christophe Grosjean
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's working with :
UserTransaction t = (UserTransaction) ctx.lookup("javax.transaction.UserTransaction");
Christophe
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!