Search in sources :

Example 41 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class CheckPersistenceUnitUsageTest method vagueMatches.

@Keys({ @Key(value = "persistenceUnitRef.vagueMatches") })
public AppModule vagueMatches() {
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(FooStatelessTwo.class));
    final EjbModule ejbModule = new EjbModule(ejbJar);
    final AppModule appModule = new AppModule(ejbModule.getClassLoader(), ejbModule.getJarLocation());
    appModule.getEjbModules().add(ejbModule);
    final PersistenceUnit pu = new PersistenceUnit("fooUnit");
    final org.apache.openejb.jee.jpa.unit.Persistence p = new org.apache.openejb.jee.jpa.unit.Persistence(pu);
    final PersistenceModule pm = new PersistenceModule("foo", p);
    appModule.getPersistenceModules().add(pm);
    final PersistenceUnit pu1 = new PersistenceUnit("fooUnit");
    final org.apache.openejb.jee.jpa.unit.Persistence p1 = new org.apache.openejb.jee.jpa.unit.Persistence(pu1);
    final PersistenceModule pm1 = new PersistenceModule("foo1", p1);
    appModule.addPersistenceModule(pm1);
    return appModule;
}
Also used : AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) PersistenceModule(org.apache.openejb.config.PersistenceModule) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) StatelessBean(org.apache.openejb.jee.StatelessBean) EjbJar(org.apache.openejb.jee.EjbJar)

Example 42 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class CheckPersistenceContextUsageTest method vagueMatches.

@Keys({ @Key(value = "persistenceContextRef.vagueMatches") })
public AppModule vagueMatches() {
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(FooStatelessTwo.class));
    final EjbModule ejbModule = new EjbModule(ejbJar);
    final AppModule appModule = new AppModule(ejbModule.getClassLoader(), ejbModule.getJarLocation());
    appModule.getEjbModules().add(ejbModule);
    final PersistenceUnit pu = new PersistenceUnit("fooUnit");
    final org.apache.openejb.jee.jpa.unit.Persistence p = new org.apache.openejb.jee.jpa.unit.Persistence(pu);
    final PersistenceModule pm = new PersistenceModule("foo", p);
    appModule.getPersistenceModules().add(pm);
    final PersistenceUnit pu1 = new PersistenceUnit("fooUnit");
    final org.apache.openejb.jee.jpa.unit.Persistence p1 = new org.apache.openejb.jee.jpa.unit.Persistence(pu1);
    final PersistenceModule pm1 = new PersistenceModule("foo1", p1);
    appModule.addPersistenceModule(pm1);
    return appModule;
}
Also used : AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) PersistenceModule(org.apache.openejb.config.PersistenceModule) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) StatelessBean(org.apache.openejb.jee.StatelessBean) EjbJar(org.apache.openejb.jee.EjbJar)

Example 43 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class CheckPersistenceContextUsageTest method noUnitName.

@Keys({ @Key(value = "persistenceContextRef.noUnitName"), @Key(value = "persistenceContextRef.noMatches") })
public AppModule noUnitName() {
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(FooStatelessOne.class));
    final EjbModule ejbModule = new EjbModule(ejbJar);
    final AppModule appModule = new AppModule(ejbModule.getClassLoader(), ejbModule.getJarLocation());
    appModule.getEjbModules().add(ejbModule);
    final PersistenceUnit pu = new PersistenceUnit("fooUnit");
    final PersistenceUnit pu1 = new PersistenceUnit("fooUnit1");
    final PersistenceUnit pu2 = new PersistenceUnit("fooUnit");
    final org.apache.openejb.jee.jpa.unit.Persistence p = new org.apache.openejb.jee.jpa.unit.Persistence(pu, pu1, pu2);
    final PersistenceModule pm = new PersistenceModule("foo", p);
    appModule.addPersistenceModule(pm);
    return appModule;
}
Also used : AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) PersistenceModule(org.apache.openejb.config.PersistenceModule) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) StatelessBean(org.apache.openejb.jee.StatelessBean) EjbJar(org.apache.openejb.jee.EjbJar)

Example 44 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class DynamicDataSourceTest method route.

@Test
public void route() throws Exception {
    System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    // resources
    for (int i = 1; i <= 3; i++) {
        final String dbName = "database" + i;
        final Resource resourceDs = new Resource(dbName, "DataSource");
        final Properties p = resourceDs.getProperties();
        p.put("JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("JdbcUrl", "jdbc:hsqldb:mem:db" + i);
        p.put("UserName", "sa");
        p.put("Password", "");
        p.put("JtaManaged", "true");
        assembler.createResource(config.configureService(resourceDs, ResourceInfo.class));
    }
    final Resource resourceRouter = new Resource("My Router", "org.apache.openejb.router.test.DynamicDataSourceTest$DeterminedRouter", "org.router:DeterminedRouter");
    resourceRouter.getProperties().setProperty("DatasourceNames", "database1 database2 database3");
    resourceRouter.getProperties().setProperty("DefaultDataSourceName", "database1");
    assembler.createResource(config.configureService(resourceRouter, ResourceInfo.class));
    final Resource resourceRoutedDs = new Resource("Routed Datasource", "org.apache.openejb.resource.jdbc.Router", "RoutedDataSource");
    resourceRoutedDs.getProperties().setProperty("Router", "My Router");
    assembler.createResource(config.configureService(resourceRoutedDs, ResourceInfo.class));
    // containers
    final StatelessSessionContainerInfo statelessContainerInfo = config.configureService(StatelessSessionContainerInfo.class);
    assembler.createContainer(statelessContainerInfo);
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(RoutedEJBBean.class));
    ejbJar.addEnterpriseBean(new StatelessBean(UtilityBean.class));
    final EjbModule ejbModule = new EjbModule(ejbJar);
    // Create an "ear"
    final AppModule appModule = new AppModule(ejbModule.getClassLoader(), "test-dynamic-data-source");
    appModule.getEjbModules().add(ejbModule);
    // Create a persistence-units
    final PersistenceUnit unit = new PersistenceUnit("router");
    unit.addClass(Person.class);
    unit.getProperties().put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
    unit.setTransactionType(TransactionType.JTA);
    unit.setJtaDataSource("Routed Datasource");
    appModule.addPersistenceModule(new PersistenceModule("root", new Persistence(unit)));
    for (int i = 1; i <= 3; i++) {
        final PersistenceUnit u = new PersistenceUnit("db" + i);
        u.addClass(Person.class);
        u.getProperties().put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
        u.setTransactionType(TransactionType.JTA);
        u.setJtaDataSource("database" + i);
        appModule.addPersistenceModule(new PersistenceModule("root", new Persistence(u)));
    }
    assembler.createApplication(config.configureApplication(appModule));
    // context
    final Context ctx = new InitialContext();
    // running persist on all "routed" databases
    final List<String> databases = new ArrayList<>();
    databases.add("database1");
    databases.add("database2");
    databases.add("database3");
    // convinient bean to create tables for each persistence unit
    final Utility utility = (Utility) ctx.lookup("UtilityBeanLocal");
    utility.initDatabase();
    final RoutedEJB ejb = (RoutedEJB) ctx.lookup("RoutedEJBBeanLocal");
    for (int i = 0; i < 18; i++) {
        final String name = "record " + i;
        final String db = databases.get(i % 3);
        ejb.persist(i, name, db);
    }
    // assert database records number using jdbc
    for (int i = 1; i <= 3; i++) {
        final Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:db" + i, "sa", "");
        final Statement st = connection.createStatement();
        final ResultSet rs = st.executeQuery("select count(*) from \"DynamicDataSourceTest$Person\"");
        rs.next();
        assertEquals(6, rs.getInt(1));
        st.close();
        connection.close();
    }
}
Also used : StatelessSessionContainerInfo(org.apache.openejb.assembler.classic.StatelessSessionContainerInfo) AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) ArrayList(java.util.ArrayList) LocalInitialContextFactory(org.apache.openejb.core.LocalInitialContextFactory) Properties(java.util.Properties) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) ResultSet(java.sql.ResultSet) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) EjbJar(org.apache.openejb.jee.EjbJar) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) PersistenceContext(javax.persistence.PersistenceContext) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) Statement(java.sql.Statement) Resource(org.apache.openejb.config.sys.Resource) Connection(java.sql.Connection) PersistenceModule(org.apache.openejb.config.PersistenceModule) InitialContext(javax.naming.InitialContext) Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) StatelessBean(org.apache.openejb.jee.StatelessBean) Assembler(org.apache.openejb.assembler.classic.Assembler) Test(org.junit.Test)

Example 45 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class DataSourceInitialSizeAdjustingTest method persistence.

@Module
public Persistence persistence() throws Exception {
    final PersistenceUnit unit = new PersistenceUnit("foo-unit");
    unit.setJtaDataSource("DataSourceInitialSizeAdjustingTest");
    return new Persistence(unit);
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) Module(org.apache.openejb.testing.Module)

Aggregations

PersistenceUnit (org.apache.openejb.jee.jpa.unit.PersistenceUnit)48 Persistence (org.apache.openejb.jee.jpa.unit.Persistence)36 Module (org.apache.openejb.testing.Module)19 AppModule (org.apache.openejb.config.AppModule)10 EjbJar (org.apache.openejb.jee.EjbJar)10 AppInfo (org.apache.openejb.assembler.classic.AppInfo)9 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)9 EjbModule (org.apache.openejb.config.EjbModule)9 PersistenceModule (org.apache.openejb.config.PersistenceModule)8 WebApp (org.apache.openejb.jee.WebApp)7 ArrayList (java.util.ArrayList)5 StatelessBean (org.apache.openejb.jee.StatelessBean)5 File (java.io.File)4 Properties (java.util.Properties)4 URL (java.net.URL)3 Collection (java.util.Collection)3 List (java.util.List)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 Assembler (org.apache.openejb.assembler.classic.Assembler)3 PersistenceUnitInfo (org.apache.openejb.assembler.classic.PersistenceUnitInfo)3