Search in sources :

Example 16 with CommonDataSource

use of javax.sql.CommonDataSource in project requery by requery.

the class CompletableEntityStoreTest method setup.

@Before
public void setup() throws SQLException {
    Platform platform = new HSQL();
    CommonDataSource dataSource = DatabaseType.getDataSource(platform);
    EntityModel model = io.requery.test.model.Models.DEFAULT;
    final TransactionListener transactionListener = new TransactionListener() {

        @Override
        public void beforeBegin(TransactionIsolation isolation) {
        }

        @Override
        public void afterBegin(TransactionIsolation isolation) {
            transactionState = TransactionState.BEGIN;
        }

        @Override
        public void beforeCommit(Set<Type<?>> types) {
        }

        @Override
        public void afterCommit(Set<Type<?>> types) {
            transactionState = TransactionState.COMMIT;
        }

        @Override
        public void beforeRollback(Set<Type<?>> types) {
        }

        @Override
        public void afterRollback(Set<Type<?>> types) {
            transactionState = TransactionState.ROLLBACK;
        }
    };
    Configuration configuration = new ConfigurationBuilder(dataSource, model).useDefaultLogging().setStatementCacheSize(10).setBatchUpdateSize(50).setWriteExecutor(Executors.newSingleThreadExecutor()).addTransactionListenerFactory(new Supplier<TransactionListener>() {

        @Override
        public TransactionListener get() {
            return transactionListener;
        }
    }).build();
    data = new CompletableEntityStore<>(new EntityDataStore<Persistable>(configuration));
    SchemaModifier tables = new SchemaModifier(configuration);
    tables.createTables(TableCreationMode.DROP_CREATE);
}
Also used : TransactionListener(io.requery.TransactionListener) ConfigurationBuilder(io.requery.sql.ConfigurationBuilder) HashSet(java.util.HashSet) Set(java.util.Set) Platform(io.requery.sql.Platform) Configuration(io.requery.sql.Configuration) HSQL(io.requery.sql.platform.HSQL) TransactionIsolation(io.requery.TransactionIsolation) EntityModel(io.requery.meta.EntityModel) CommonDataSource(javax.sql.CommonDataSource) SchemaModifier(io.requery.sql.SchemaModifier) EntityDataStore(io.requery.sql.EntityDataStore) Supplier(io.requery.util.function.Supplier) Before(org.junit.Before)

Example 17 with CommonDataSource

use of javax.sql.CommonDataSource in project requery by requery.

the class UpsertTest method setup.

@Before
public void setup() throws SQLException {
    CommonDataSource dataSource = DatabaseType.getDataSource(platform);
    EntityModel model = Models.MODEL3;
    Configuration configuration = new ConfigurationBuilder(dataSource, model).useDefaultLogging().setEntityCache(new EmptyEntityCache()).setWriteExecutor(Executors.newSingleThreadExecutor()).build();
    SchemaModifier tables = new SchemaModifier(configuration);
    tables.createTables(TableCreationMode.DROP_CREATE);
    System.out.println(tables.createTablesString(TableCreationMode.DROP_CREATE));
    data = new EntityDataStore<>(configuration);
}
Also used : ConfigurationBuilder(io.requery.sql.ConfigurationBuilder) Configuration(io.requery.sql.Configuration) EmptyEntityCache(io.requery.cache.EmptyEntityCache) EntityModel(io.requery.meta.EntityModel) CommonDataSource(javax.sql.CommonDataSource) SchemaModifier(io.requery.sql.SchemaModifier) Before(org.junit.Before)

Example 18 with CommonDataSource

use of javax.sql.CommonDataSource in project tomee by apache.

the class FlushableDataSourceHandlerTest method checkIt.

@Test
public void checkIt() throws IOException {
    assertThat(ds, instanceOf(Flushable.class));
    assertThat(ds, instanceOf(DataSource.class));
    final FlushableDataSourceHandler handler = FlushableDataSourceHandler.class.cast(Proxy.getInvocationHandler(ds));
    final CommonDataSource delegate = handler.getDelegate();
    assertNotNull(delegate);
    assertFalse(BasicDataSource.class.cast(delegate).isClosed());
    Flushable.class.cast(ds).flush();
    assertTrue(BasicDataSource.class.cast(delegate).isClosed());
    final CommonDataSource newDelegate = handler.getDelegate();
    assertFalse(BasicDataSource.class.cast(newDelegate).isClosed());
    assertNotSame(newDelegate, delegate);
}
Also used : Flushable(java.io.Flushable) CommonDataSource(javax.sql.CommonDataSource) BasicDataSource(org.apache.openejb.resource.jdbc.dbcp.BasicDataSource) CommonDataSource(javax.sql.CommonDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 19 with CommonDataSource

use of javax.sql.CommonDataSource in project tomee by apache.

the class PersistenceBuilder method createEntityManagerFactory.

public ReloadableEntityManagerFactory createEntityManagerFactory(final PersistenceUnitInfo info, final ClassLoader classLoader, final Map<ComparableValidationConfig, ValidatorFactory> validators, final boolean hasCdi) throws Exception {
    final PersistenceUnitInfoImpl unitInfo = new PersistenceUnitInfoImpl(persistenceClassLoaderHandler);
    // Persistence Unit Id
    unitInfo.setId(info.id);
    // Persistence Unit Name
    unitInfo.setPersistenceUnitName(info.name);
    // Persistence Provider Class Name
    unitInfo.setPersistenceProviderClassName(info.provider);
    // ClassLoader
    unitInfo.setClassLoader(classLoader);
    // Exclude Unlisted Classes
    unitInfo.setExcludeUnlistedClasses(info.excludeUnlistedClasses);
    unitInfo.setLazilyInitialized(info.webappName != null || "true".equalsIgnoreCase(info.properties.getProperty("tomee.jpa.factory.lazy", SystemInstance.get().getProperty("tomee.jpa.factory.lazy", "false"))));
    final Context context = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
    // JTA Datasource
    String jtaDataSourceId = info.jtaDataSource;
    unitInfo.setJtaDataSourceName(jtaDataSourceId);
    if (jtaDataSourceId != null) {
        if (!SystemInstance.get().hasProperty("openejb.geronimo")) {
            final String initialJndiName = jtaDataSourceId;
            try {
                if (!jtaDataSourceId.startsWith("java:openejb/Resource/") && !jtaDataSourceId.startsWith("openejb/Resource/")) {
                    jtaDataSourceId = "openejb/Resource/" + jtaDataSourceId;
                }
                final CommonDataSource jtaDataSource = (CommonDataSource) context.lookup(jtaDataSourceId);
                unitInfo.setJtaDataSource(jtaDataSource);
            } catch (final NamingException e) {
                try {
                    unitInfo.setJtaDataSource((DataSource) new InitialContext().lookup(initialJndiName));
                } catch (final NamingException ne) {
                    throw new OpenEJBException("Could not lookup <jta-data-source> '" + jtaDataSourceId + "' for unit '" + unitInfo.getPersistenceUnitName() + "'", e);
                }
            }
        }
    }
    // Managed Class Names
    unitInfo.setManagedClassNames(info.classes);
    // Mapping File Names
    unitInfo.setMappingFileNames(info.mappingFiles);
    // Handle Properties
    unitInfo.setProperties(info.properties);
    // Schema version of the persistence.xml file
    unitInfo.setPersistenceXMLSchemaVersion(info.persistenceXMLSchemaVersion);
    // Second-level cache mode for the persistence unit
    final SharedCacheMode sharedCacheMode = Enum.valueOf(SharedCacheMode.class, info.sharedCacheMode);
    unitInfo.setSharedCacheMode(sharedCacheMode);
    // The validation mode to be used for the persistence unit
    final ValidationMode validationMode = Enum.valueOf(ValidationMode.class, info.validationMode);
    unitInfo.setValidationMode(validationMode);
    // Persistence Unit Transaction Type
    final PersistenceUnitTransactionType type = Enum.valueOf(PersistenceUnitTransactionType.class, info.transactionType);
    unitInfo.setTransactionType(type);
    // Non JTA Datasource
    String nonJtaDataSourceId = info.nonJtaDataSource;
    unitInfo.setNonJtaDataSourceName(nonJtaDataSourceId);
    if (nonJtaDataSourceId != null) {
        if (!SystemInstance.get().hasProperty("openejb.geronimo")) {
            final String initialJndiName = nonJtaDataSourceId;
            try {
                if (!nonJtaDataSourceId.startsWith("java:openejb/Resource/")) {
                    nonJtaDataSourceId = "java:openejb/Resource/" + nonJtaDataSourceId;
                }
                final CommonDataSource nonJtaDataSource = (CommonDataSource) context.lookup(nonJtaDataSourceId);
                unitInfo.setNonJtaDataSource(nonJtaDataSource);
            } catch (final NamingException e) {
                try {
                    unitInfo.setNonJtaDataSource((DataSource) new InitialContext().lookup(initialJndiName));
                } catch (final NamingException ne) {
                    throw new OpenEJBException("Could not lookup <non-jta-data-source> '" + nonJtaDataSourceId + "' for unit '" + unitInfo.getPersistenceUnitName() + "'", e);
                }
            }
        }
    }
    // Persistence Unit Root Url
    unitInfo.setRootUrlAndJarUrls(info.persistenceUnitRootUrl, info.jarFiles);
    // create the persistence provider
    final String persistenceProviderClassName = unitInfo.getPersistenceProviderClassName();
    unitInfo.setPersistenceProviderClassName(persistenceProviderClassName);
    final EntityManagerFactoryCallable callable = new EntityManagerFactoryCallable(persistenceProviderClassName, unitInfo, classLoader, validators, hasCdi);
    return new ReloadableEntityManagerFactory(classLoader, callable, unitInfo);
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) ContainerSystem(org.apache.openejb.spi.ContainerSystem) OpenEJBException(org.apache.openejb.OpenEJBException) PersistenceUnitInfoImpl(org.apache.openejb.persistence.PersistenceUnitInfoImpl) ValidationMode(javax.persistence.ValidationMode) CommonDataSource(javax.sql.CommonDataSource) InitialContext(javax.naming.InitialContext) CommonDataSource(javax.sql.CommonDataSource) DataSource(javax.sql.DataSource) SharedCacheMode(javax.persistence.SharedCacheMode) NamingException(javax.naming.NamingException) PersistenceUnitTransactionType(javax.persistence.spi.PersistenceUnitTransactionType)

Example 20 with CommonDataSource

use of javax.sql.CommonDataSource in project tomee by apache.

the class DataSourceFactory method wrapIfNeeded.

private static CommonDataSource wrapIfNeeded(final String handler, final CommonDataSource ds) throws InstantiationException, IllegalAccessException {
    if (handler != null) {
        try {
            final Class<?> handlerClass = Thread.currentThread().getContextClassLoader().loadClass(handler);
            InvocationHandler instance;
            try {
                instance = InvocationHandler.class.cast(handlerClass.getConstructor(DataSource.class).newInstance(ds));
            } catch (final InvocationTargetException e) {
                throw new IllegalStateException(e.getCause());
            } catch (final NoSuchMethodException e) {
                try {
                    instance = InvocationHandler.class.cast(handlerClass.getConstructor(CommonDataSource.class).newInstance(ds));
                } catch (final InvocationTargetException e2) {
                    throw new IllegalStateException(e.getCause());
                } catch (final NoSuchMethodException e2) {
                    instance = InvocationHandler.class.cast(handlerClass.newInstance());
                }
            }
            return wrapWithHandler(ds, instance);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("Can't find handler: " + handler, e);
        }
    }
    return ds;
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) XADataSource(javax.sql.XADataSource) DataSource(javax.sql.DataSource) LoggingSqlDataSource(org.apache.openejb.resource.jdbc.logging.LoggingSqlDataSource) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) CommonDataSource(javax.sql.CommonDataSource)

Aggregations

CommonDataSource (javax.sql.CommonDataSource)23 EntityModel (io.requery.meta.EntityModel)12 Configuration (io.requery.sql.Configuration)12 ConfigurationBuilder (io.requery.sql.ConfigurationBuilder)12 SchemaModifier (io.requery.sql.SchemaModifier)12 Before (org.junit.Before)12 EntityCacheBuilder (io.requery.cache.EntityCacheBuilder)6 Platform (io.requery.sql.Platform)5 HSQL (io.requery.sql.platform.HSQL)5 CacheManager (javax.cache.CacheManager)5 CachingProvider (javax.cache.spi.CachingProvider)5 EntityDataStore (io.requery.sql.EntityDataStore)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 DataSource (javax.sql.DataSource)4 XADataSource (javax.sql.XADataSource)4 EmptyEntityCache (io.requery.cache.EmptyEntityCache)3 TableCreationMode (io.requery.sql.TableCreationMode)3 SQLite (io.requery.sql.platform.SQLite)3 SQLException (java.sql.SQLException)3 Flushable (java.io.Flushable)2