11 Replies Latest reply on Jan 23, 2007 8:48 AM by rookie1977

    Transaction via JNDI

    rookie1977

      How can I get a javax.transaction.Transaction reference via JNDI? Is this something already
      provided by JBoss TS or I must implement this by myself?

        • 1. Re: Transaction via JNDI
          kconner

          Do you mean the current transaction? If so lookup the TransactionManager and use the getTransaction() method.

          • 2. Re: Transaction via JNDI
            rookie1977

            That is what I am looking for, but I am looking for "independance" from JBossTS, so that I can transparently fetch Transaction from JNDI instead of

            javax.transaction.TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();

            javax.transaction.Transaction transaction = transactionManager.getTransaction();

            • 3. Re: Transaction via JNDI
              kconner

              You have independence, you retrieve the transaction manager from JNDI :-)

              • 4. Re: Transaction via JNDI
                kconner

                Sorry, should have added the JNDI name (java:/TransactionManager).

                • 5. Re: Transaction via JNDI
                  rookie1977

                  Thank you very much for quick response.

                  Greetings :)

                  • 6. Re: Transaction via JNDI
                    kconner

                    No problem

                    • 7. Re: Transaction via JNDI
                      rookie1977

                      I still have a problem. When I retrieve the object bound to the "java:/TransactionManager" name I get javax.naming.Reference.

                      Reference ref = (Reference) new InitialContext().lookup("java:/TransactionManager");

                      However the ref.size() returns 0 so I can not get the object that is underlying this logical name.

                      • 8. Re: Transaction via JNDI
                        rookie1977

                        Note that I have setup-ed the JNDI context, using -D parameters (java.naming.factory.initial, java.naming.factory.url.pkgs).

                        • 9. Re: Transaction via JNDI
                          rookie1977

                          Here is my little contribution for this problem. Thanks to the http://mail-archives.apache.org/mod_mbox/tomcat-dev/200501.mbox/%3CEBCEA7135D99624F973B12302C3717FF06C65D6E@scenm1.sysadmin.suny.edu%3E

                          public static TransactionManager getObjectInstance(Reference reference) throws Exception {
                          
                           String factoryClassName = reference.getFactoryClassName();
                           String className = reference.getClassName();
                          
                           log.warn("Factory Class Name=" + factoryClassName);
                           log.warn("Class Name=" + className);
                          
                           final Class factoryClass;
                          
                           ClassLoader tcl = Thread.currentThread().getContextClassLoader();
                           if (tcl != null) {
                           log.warn("Using the context class loader");
                           try {
                           factoryClass = tcl.loadClass(factoryClassName);
                           } catch (ClassNotFoundException exception) {
                           log.warn("1", exception);
                           NamingException namingException = new NamingException(
                           "Could not load resource factory class '"
                           + factoryClassName
                           + "' using the context class loader,ClassNotFoundException:"
                           + exception.getMessage());
                           namingException.setRootCause(exception);
                           throw namingException;
                           }
                           } else {
                           try {
                           factoryClass = Class.forName(factoryClassName);
                           } catch (ClassNotFoundException exception) {
                           log.warn("Could not load resource factory class", exception);
                           NamingException namingException = new NamingException(
                           "Could not load resource factory class '"
                           + factoryClassName
                           + "' using the JVM class loader,ClassNotFoundException:"
                           + exception.getMessage());
                           namingException.setRootCause(exception);
                           throw namingException;
                           }
                           }
                          
                           final ObjectFactory factory;
                           try {
                           factory = (ObjectFactory) factoryClass.newInstance();
                           } catch (Throwable throwable) {
                           log.warn("Could notinstantiate factory class", throwable);
                           NamingException namingException = new NamingException(
                           "Could notinstantiate factory class '"
                           + factoryClass.toString() + "': "
                           + throwable.getMessage());
                           namingException.setRootCause(throwable);
                           throw namingException;
                           }
                          
                           Object newObject = factory.getObjectInstance(reference, null, null,null);
                           if(newObject!=null) {
                           log.warn("Returning object of type = " + newObject.getClass().getName());
                           }
                          
                           TransactionManager transactionManager = (TransactionManager) newObject;
                           return transactionManager;
                           }


                          • 10. Re: Transaction via JNDI
                            kconner

                            Yes, the transaction manager is bound as a reference.

                            I'm surprised you had to handle it in the above fashion though, this is normally handled by the app server lookup.

                            Can you describe your setup?

                            • 11. Re: Transaction via JNDI
                              rookie1977

                              I am running stand alone, non-managed environment (no EJB container) for some prototype system. Just a bunch of main methods (recovery, server, client)...