Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

lookup on UserTransaction

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's working with :
UserTransaction t = (UserTransaction) ctx.lookup("javax.transaction.UserTransaction");
Christophe
reply
    Bookmark Topic Watch Topic
  • New Topic