Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
java
java
borderStylesolid
titleGeronimoTransactionManagerLookup
package org.hibernate.transaction;

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import javax.transaction.TransactionManager;
import org.hibernate.HibernateException;
import org.hibernate.transaction.TransactionManagerLookup;

import org.apache.geronimo.gbean.AbstractName;
import org.apache.geronimo.gbean.AbstractNameQuery;
import org.apache.geronimo.kernel.Kernel;
import org.apache.geronimo.kernel.KernelRegistry;
import org.apache.geronimo.kernel.proxy.ProxyManager;

public class GeronimoTransactionManagerLookup implements
        TransactionManagerLookup {

    public static final String UserTransactionName = "java:comp/UserTransaction";

    public Object getTransactionIdentifier(Transaction arg0) {
		return null;
    }

    public TransactionManager getTransactionManager(Properties props) throws HibernateException {
       /*
         * try { Kernel kernel = KernelRegistry.getSingleKernel(); ProxyManager
         * proxyManager = kernel.getProxyManager(); AbstractNameQuery query =
         * new AbstractNameQuery(TransactionManager.class.getName()); Set names =
         * kernel.listGBeans(query); AbstractName name = null; for (Iterator it =
         * names.iterator(); it.hasNext();) name = (AbstractName) it.next();
         * Object transMg = (Object) proxyManager.createProxy(name,
         * TransactionManager.class); return (TransactionManager)transMg; }catch
         * (Exception e) { e.printStackTrace(); System.out.println(); throw new
         * HibernateException("Geronimo Transaction Manager Lookup Failed", e); }
         */
       try {
       Kernel kernel = KernelRegistry.getSingleKernel();
       AbstractNameQuery query = new AbstractNameQuery(TransactionManager.class.getName ());
       Set<AbstractName> names = kernel.listGBeans(query);
       if (names.size() != 1) {
           throw new IllegalStateException("Expected one transaction manager, not " + names.size());
       }
       AbstractName name = names.iterator().next();
       TransactionManager transMg = (TransactionManager)
       kernel.getGBean(name);
       return (TransactionManager)transMg;
       } catch (Exception e) {
           e.printStackTrace();
           System.out.println();
           throw new HibernateException("Geronimo Transaction Manager Lookup Failed", e);
       }
   }

    public String getUserTransactionName() {
        return UserTransactionName;
    }
}

...

  • Add the hibernate jar to the classpath. You will now need to build hibernate with the GeronimoTransactionManagerLookup class. If you have not downloaded the hibernate source you can compile the class after putting the hibernate jar in the classpath and then manually add that class to the hibernate jar file.
    set CLASSPATH=%CLASSPATH%;<hibernate_home>/lib/hibernate3.jar
  • Add the Geronimo kernel to your classpath
    set CLASSPATH=%CLASSPATH%;<geronimo_home>/lib/geronimo-kernel-2.0.12.jar
  • To add the class manually, although not needed for this particular sample, you can use the tool of your preference and add the GeronimoTransactionManagerLookup.class to the org\hibernate\transaction directory in the hibernate_home>/lib/hibernate3.jar file.

...