3 Replies Latest reply on Jan 16, 2004 9:45 AM by pichler.mi

    UserTransaction throws RuntimeException in client

    wufka

      Hi everybody!

      I try to use client-demarcated transactions with jboss3.0alpha (with hypersonic). Creating an InitialContext works fine, and I am also able to retrieve and use the home interface of beans. The lookup

      userTransaction = (UserTransaction) context.lookup("UserTransaction");

      also seems to work fine (the result is not null). When I call

      userTransaction.begin(),

      however, the following Exception is thrown:

      java.lang.RuntimeException: UT factory lookup failed: javax.naming.NoIntitialContextException: 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.

      My client code looks as follows:

      Properties p = new Properties();
      p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
      p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      try {
      context = new InitialContext(p);
      }
      ...
      userTransaction = (UserTransaction) context.lookup("UserTransaction");
      ...
      userTransaction.begin();

      Does anybody have an idea what the problem might be? Everything else seems to work; the client can create, find and remove beans just fine as long as it doesn't use UserTransaction.

      Any help would be appreciated!
      Michael Wufka

        • 1. Re: UserTransaction throws RuntimeException in client
          wufka

          After studying the jboss-sources, I was able to find a workaround. If I put the following call at the beginning of the main()-Method of the client, everything works fine:

          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
          System.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
          System.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

          The reason is that the begin()-method of org.jboss.tm.usertx.client.ClientUserTransaction calls the no-argument constructor InitialContext() to retrieve a UserTransactionSessionFactory. For this to work, the above shown system properties have to be set.

          My follow-up question is: Is this a bug or a feature, i.e. should every client (who uses a UserTransaction) have to set these properties? And if so, did I just overlook it, or is this not documented? Or is there a more elegant way to make it work?

          Thank you!
          Michael

          • 2. Re: UserTransaction throws RuntimeException in client
            johnsbrn

            use the jndi.properties files in your client classpath

            • 3. Re: UserTransaction throws RuntimeException in client
              pichler.mi

              We want to use a user transaction in a web application which should be able to run both inside jboss (.ear) or on a separate web server machine (tomcat).

              In the latter configuration, we have got the same problem: passing the JBoss properties to the InitialContext which does the UserTransaction lookup is not enough: utx.begin() then fails, thus the properties must be set as System properties first.

              However, we do not want to put jndi.properties that point to JBoss into the classpath of tomcat itself (as far as I can remember that conflicts with the tomcat internal JNDI use).

              Is there any other choice to get utx.begin() working?
              Or is this a bug in the UserTransaction code (we are using JBoss 3.2.1)?