5 Replies Latest reply on Sep 23, 2003 12:45 PM by Don Laidlaw

    transaction from the client side

    Volkan Yildiz Newbie

      Hi all,
      I have two event. The first one executed without second but it must be rolledback when something happen (power cut!) before the second one finished.So is it possible to begin a transaction and wait until some other transactions .

        • 1. Re: transaction from the client side
          Don Laidlaw Newbie

          Yes.

          Assuming your client code is a servlet or standalone program get a JNDI context with:

          InitialContext jndi = new InitialContext();
          or InitialContext jndi = new InitialContext(map);

          where map specifies how to connect to JNDI.

          Then use the jndi object to get a UserTransaction, like:

          UserTransaction utx = (UserTransaction) jndi.lookup("UserTransaction");

          After that you can use the methods on the utx object to start, commit and rollback the transaction. All transacted methods you call in betwee start() and commit() or rollback() will happen inside the scope of the UserTransaction utx.

          More info on UserTransaction and getting a JNDI context can be found in the manuals or in a good EJB book.

          • 2. Re: transaction from the client side
            Volkan Yildiz Newbie

            Then i got the following exception.Any idea?


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

            • 3. Re: transaction from the client side
              Don Laidlaw Newbie

              You are not getting a correct JNDI InitialContext. Try creating a file called jndi.properties in your classpath for the client code and add the following lines to it:

              java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
              java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
              java.naming.provider.url=localhost:1099

              You will have to change localhost to the hostname running JBoss if it is not the same host as the client code.

              • 4. Re: transaction from the client side
                Volkan Yildiz Newbie

                I have tried this and it gave me the same exception. I'm using version 3.0.6. May it be something about it.

                Properties prop = new Properties();
                prop.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                prop.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
                prop.put("java.naming.provider.url", "localhost:1099");
                InitialContext jndi = new InitialContext(prop);

                UserTransaction utx = (UserTransaction) jndi.lookup("UserTransaction");

                System.out.println("start");
                utx.begin();

                • 5. Re: transaction from the client side
                  Don Laidlaw Newbie

                  That is possible, I don't have that version. But it is more likely that you don't have the client jars for jboss in your classpath. Including the jndi jars. Try adding some jars from the JBOSS_HOME/client directory to your classpath.