public class BootstrapServletListener extends GuiceServletContextListener { protected Injector getInjector() { final PersistenceModule persistenceModule = new PersistenceModule() { @Override protected void configurePersistence() { bindContainerManagedPersistenceUnitWithJndiName( "java:comp/env/foobar/mainPuJndiName" ) .annotatedWith(MainPU.class) .useGlobalTransactionWithJndiName( "java:comp/env/UserTransaction" ); bindContainerManagedPersistenceUnitWithJndiName( "java:comp/env/foobar/alternativePUJndiName" ) .annotatedWith(AlternativePU.class) .useGlobalTransactionWithJndiName( "java:comp/env/UserTransaction" ); bindContainerManagedPersistenceUnitWithJndiName( "java:comp/env/foobar/systemPuJndiName" ) .annotatedWith(SystemPU.class) .useLocalTransaction(); } }; final ServletModule servletModule = new ServletModule() { filter("/*").through(PersistenceFilter.class); }; return Guice.createInjector( servletModule, persistenceModule, getApplicationSpecificModules() ); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>FooBar App</display-name> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>foo.bar.BootstrapServletListener</listener-class> </listener> <!-- More config. --> </web-app>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="mainPuName" transaction-type="JTA"> <!-- your configuration --> </peristence-unit> <persistence-unit name="alternativePuName" transaction-type="JTA"> <!-- your configuration --> </peristence-unit> <persistence-unit name="systemPuName" transaction-type="RESOURCE_LOCAL"> <!-- your configuration --> </peristence-unit> </peristence>
In order to allow container managed persistence units the persistence unit must be bound in the JNDI framework.
If your container follows the J2EE Standard 5.0 add the following to your web.xml
<peristence-unit-ref> <peristence-unit-ref-name>foobar/mainPuJndiName</peristence-unit-ref-name> <peristence-unit-name>mainPuName</peristence-unit-name> </peristence-unit-ref> <peristence-unit-ref> <peristence-unit-ref-name>foobar/alternativePuJndiName</peristence-unit-ref-name> <peristence-unit-name>alterantivePuName</peristence-unit-name> </peristence-unit-ref> <peristence-unit-ref> <peristence-unit-ref-name>foobar/systemPuJndiName</peristence-unit-ref-name> <peristence-unit-name>systemPuName</peristence-unit-name> </peristence-unit-ref>
Important: JBoss AS7.x do not support placing the JNDI binding in the web.xml. Instead add the following to your persistence.xml
<property name="jboss.entity.manager.factory.jndi.name" value="java:comp/env/foobar/mainPuJndiName" /> <property name="jboss.as.jpa.managed" value="true" /> <property name="jboss.entity.manager.factory.jndi.name" value="java:comp/env/foobar/alternativePuJndiName" /> <property name="jboss.as.jpa.managed" value="true" /> <property name="jboss.entity.manager.factory.jndi.name" value="java:comp/env/foobar/systemPuJndiName" /> <property name="jboss.as.jpa.managed" value="true" />
Important: JBoss AS 7.x makes all datasources defined in the standalone.xml JTA managed. If you want one or more of the datasources to be resource local you must add the attribute "jta" to the datasource element.
<datasource jndi-name="java:jboss/datasources/systemDS" pool-name="systemDS" enabled="true" use-java-context="true" jta="false"> ... </datasource>