------ Multi Datasource Web Application ------ The Apache Onami developers team ------ 2014 ~~ ~~ Licensed to the Apache Software Foundation (ASF) under one ~~ or more contributor license agreements. See the NOTICE file ~~ distributed with this work for additional information ~~ regarding copyright ownership. The ASF licenses this file ~~ to you under the Apache License, Version 2.0 (the ~~ "License"); you may not use this file except in compliance ~~ with the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, ~~ software distributed under the License is distributed on an ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations ~~ under the License. ~~ Multi Datasource Web Application * java code +--------------------------------------+ 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() ); } } +--------------------------------------+ * web.xml +--------------------------------------+ FooBar App guiceFilter com.google.inject.servlet.GuiceFilter guiceFilter /* foo.bar.BootstrapServletListener +--------------------------------------+ * persistence.xml +--------------------------------------+ +--------------------------------------+ * JNDI Binding 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 +--------------------------------------+ foobar/mainPuJndiName mainPuName foobar/alternativePuJndiName alterantivePuName foobar/systemPuJndiName systemPuName +--------------------------------------+ Important: JBoss AS7.x do not support placing the JNDI binding in the web.xml. Instead add the following to your persistence.xml +--------------------------------------+ +--------------------------------------+ 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. +--------------------------------------+ ... +--------------------------------------+ * Structure of the .war +--------------------------------------+ + app.war | + META-INF | | + MANIFEST.MF | | + persistence.xml | + WEB-INF | | + classes | | | + META-INF | | | | + MANIFEST.MF | | | + foo | | | | + bar | | | | | + BootstrapServletListener.class | | + lib | | | + guice.jar | | | + onami-persist.jar +--------------------------------------+