public String resolveDataSourceName(CommonDataSource dataSource) { if (dataSources == null) { this.dataSources = applicationContext.getBeansOfType(DataSource.class); } return dataSources.entrySet() .stream() .filter(entry -> { DataSource candidate = entry.getValue(); if (candidate instanceof DecoratedDataSource) { return matchesDataSource((DecoratedDataSource) candidate, dataSource); } return candidate == dataSource; }) .findFirst() .map(Entry::getKey) .orElse("dataSource"); }
public boolean isWrapperFor(Class<?> iface) throws SQLException { if (iface.isInstance(this)) { return true; } CommonDataSource cds = getUnwrappedDataSource(); if (iface.isInstance(cds)) { return true; } if (cds instanceof DataSource) { DataSource ds = (DataSource) cds; if (ds.isWrapperFor(iface)) { return true; } } return false; }
public <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isInstance(this)) { return iface.cast(this); } CommonDataSource cds = getUnwrappedDataSource(); if (iface.isInstance(cds)) { return iface.cast(cds); } if (cds instanceof DataSource) { DataSource ds = (DataSource) cds; if (ds.isWrapperFor(iface)) { return ds.unwrap(iface); } } throw new SQLException("Not a wrapper for " + iface.getName()); }
@Before public void setup() throws SQLException { Platform platform = new HSQL(); CommonDataSource dataSource = DatabaseType.getDataSource(platform); EntityModel model = Models.MODEL2; Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() .setEntityCache(new EmptyEntityCache()) .setWriteExecutor(Executors.newSingleThreadExecutor()) .build(); SchemaModifier tables = new SchemaModifier(configuration); tables.createTables(TableCreationMode.DROP_CREATE); data = new EntityDataStore<>(configuration); }
@Before public void setup() throws SQLException { CommonDataSource dataSource = DatabaseType.getDataSource(new H2()); EntityModel model = Models.JPA; CachingProvider provider = Caching.getCachingProvider(); CacheManager cacheManager = provider.getCacheManager(); Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() .setEntityCache(new EntityCacheBuilder(model) .useReferenceCache(true) .useSerializableCache(true) .useCacheManager(cacheManager) .build()) .build(); data = new EntityDataStore<>(configuration); SchemaModifier tables = new SchemaModifier(configuration); tables.dropTables(); TableCreationMode mode = TableCreationMode.CREATE; System.out.println(tables.createTablesString(mode)); tables.createTables(mode); }
@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); }
@Before public void setup() throws SQLException { Platform platform = new HSQL(); CommonDataSource dataSource = DatabaseType.getDataSource(platform); EntityModel model = io.requery.test.model.Models.DEFAULT; CachingProvider provider = Caching.getCachingProvider(); CacheManager cacheManager = provider.getCacheManager(); Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() .setWriteExecutor(Executors.newSingleThreadExecutor()) .setEntityCache(new EntityCacheBuilder(model) .useReferenceCache(true) .useSerializableCache(true) .useCacheManager(cacheManager) .build()) .build(); SchemaModifier tables = new SchemaModifier(configuration); tables.createTables(TableCreationMode.DROP_CREATE); data = new ReactorEntityStore<>(new EntityDataStore<Persistable>(configuration)); }
@Before public void setup() throws SQLException { CommonDataSource dataSource = DatabaseType.getDataSource(platform); EntityModel model = Models.STATELESS; 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); }
@Before public void setup() throws SQLException { CommonDataSource dataSource = DatabaseType.getDataSource(new SQLite()); EntityModel model = Models.AUTOVALUE; Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() .setEntityCache(new EntityCacheBuilder(model) .useReferenceCache(true) .build()) .build(); data = new EntityDataStore<>(configuration); SchemaModifier tables = new SchemaModifier(configuration); tables.dropTables(); TableCreationMode mode = TableCreationMode.CREATE_NOT_EXISTS; System.out.println(tables.createTablesString(mode)); tables.createTables(mode); }
@Before public void setup() throws SQLException { Platform platform = new HSQL(); CommonDataSource dataSource = DatabaseType.getDataSource(platform); EntityModel model = io.requery.test.model.Models.DEFAULT; CachingProvider provider = Caching.getCachingProvider(); CacheManager cacheManager = provider.getCacheManager(); Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() .setWriteExecutor(Executors.newSingleThreadExecutor()) .setEntityCache(new EntityCacheBuilder(model) .useReferenceCache(true) .useSerializableCache(true) .useCacheManager(cacheManager) .build()) .build(); SchemaModifier tables = new SchemaModifier(configuration); tables.createTables(TableCreationMode.DROP_CREATE); data = ReactiveSupport.toReactiveStore(new EntityDataStore<Persistable>(configuration)); }
@Before public void setup() throws SQLException { Platform platform = new HSQL(); CommonDataSource dataSource = DatabaseType.getDataSource(platform); EntityModel model = io.requery.test.model.Models.DEFAULT; CachingProvider provider = Caching.getCachingProvider(); CacheManager cacheManager = provider.getCacheManager(); Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() .setWriteExecutor(Executors.newSingleThreadExecutor()) .setEntityCache(new EntityCacheBuilder(model) .useReferenceCache(true) .useSerializableCache(true) .useCacheManager(cacheManager) .build()) .build(); SchemaModifier tables = new SchemaModifier(configuration); tables.createTables(TableCreationMode.DROP_CREATE); data = RxSupport.toReactiveStore(new EntityDataStore<Persistable>(configuration)); }
@Before public void setup() throws SQLException { CommonDataSource dataSource = DatabaseType.getDataSource(platform); EntityModel model = io.requery.test.model.Models.DEFAULT; Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() .setStatementCacheSize(10) .setBatchUpdateSize(50) .setWriteExecutor(Executors.newSingleThreadExecutor()) .build(); schemaModifier = new SchemaModifier(configuration); try { schemaModifier.dropTables(); } catch (Exception e) { // expected if 'drop if exists' not supported (so ignore in that case) if (!platform.supportsIfExists()) { throw e; } } schemaModifier.createTables(TableCreationMode.CREATE); }
public static CommonDataSource getJDBCDataSource(String clazz, String url, String user, String password) { if (url == null) { throw new ConnectionPoolException("url is null"); } Driver driver; try { if (clazz == null) { clazz = JdbcUtils.getDriverClassName(url); } driver = JdbcUtils.createDriver(clazz); } catch (SQLException e) { throw new ConnectionPoolException(e); } Properties connectProperties = new Properties(); if (user != null) { connectProperties.put("user", user); } if (password != null) { connectProperties.put("password", password); } return new JDBCDataSource(clazz, url, driver, connectProperties); }
@Override public CommonDataSource pool(final String name, final String driver, final Properties properties) { properties.setProperty("name", name); final String xa = String.class.cast(properties.remove("XaDataSource")); if (xa == null && !properties.containsKey("JdbcDriver")) { properties.setProperty("driverClassName", driver); } final BasicDataSource ds = build(BasicDataSource.class, properties); ds.setDriverClassName(driver); if (xa != null) { ds.setDelegate(XADataSourceResource.proxy(Thread.currentThread().getContextClassLoader(), xa)); } return ds; }
@Override public CommonDataSource pool(final String name, final String driver, final Properties properties) { return (CustomDataSource) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[]{CustomDataSource.class}, new InvocationHandler() { @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { if (method.getName().equals("name")) { return properties.getProperty("Name"); } if ("hashCode".equals(method.getName())) { return properties.hashCode(); // don't care } return null; } }); }
private static AbstractJdbcManagedConnectionFactory<?, ?, ?> create(CommonDataSource dataSource) { if (dataSource instanceof XADataSource) { return new XADataSourceMCF((XADataSource) dataSource); } else if (dataSource instanceof ConnectionPoolDataSource) { return new ConnectionPoolDataSourceMCF((ConnectionPoolDataSource) dataSource); } else if (dataSource instanceof DataSource) { return new LocalDataSourceMCF((DataSource) dataSource); } else { throw new UnsupportedOperationException(); } }
private static ConnectionProvider createConnectionProvider(CommonDataSource dataSource) { if(dataSource instanceof ConnectionPoolDataSource) { return new PooledConnectionProvider((ConnectionPoolDataSource)dataSource); } else if (dataSource instanceof DataSource) { return new DataSourceConnectionProvider((DataSource)dataSource); } else { throw new IllegalArgumentException("unsupported dataSource " + dataSource); } }
@Before public void setup() throws SQLException { CommonDataSource dataSource = DatabaseType.getDataSource(new SQLite()); EntityModel model = Models.MODEL3; Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() .setEntityCache(new WeakEntityCache()) .setWriteExecutor(Executors.newSingleThreadExecutor()) .build(); SchemaModifier tables = new SchemaModifier(configuration); tables.createTables(TableCreationMode.DROP_CREATE); data = new EntityDataStore<>(configuration); }
@Before public void setup() throws SQLException { CommonDataSource dataSource = DatabaseType.getDataSource(platform); EntityModel model = Models.DEFAULT; CachingProvider provider = Caching.getCachingProvider(); CacheManager cacheManager = provider.getCacheManager(); Configuration configuration = new ConfigurationBuilder(dataSource, model) .useDefaultLogging() // work around bug reusing prepared statements in xerial sqlite .setStatementCacheSize(platform instanceof SQLite ? 0 : 10) .setBatchUpdateSize(50) .setEntityCache(new EntityCacheBuilder(model) .useReferenceCache(true) .useSerializableCache(true) .useCacheManager(cacheManager) .build()) .build(); data = new EntityDataStore<>(configuration); SchemaModifier tables = new SchemaModifier(configuration); try { tables.dropTables(); } catch (Exception e) { // expected if 'drop if exists' not supported (so ignore in that case) if (!platform.supportsIfExists()) { throw e; } } TableCreationMode mode = TableCreationMode.CREATE; System.out.println(tables.createTablesString(mode)); tables.createTables(mode); }
@Override public String get03_DriverClassName() { CommonDataSource dataSource = this.pool.getCfgVO().getDataSource(); if (dataSource instanceof JDBCDataSource) { JDBCDataSource jdbcDataSource = (JDBCDataSource) dataSource; String clazz = jdbcDataSource.getClazz(); if (clazz != null) { return clazz; } } return "-"; }
@Override public String get04_Url() { CommonDataSource dataSource = this.pool.getCfgVO().getDataSource(); if (dataSource instanceof JDBCDataSource) { JDBCDataSource jdbcDataSource = (JDBCDataSource) dataSource; String url = jdbcDataSource.getUrl(); if (url != null) { return url; } } return "-"; }
@Override public CommonDataSource pool(final String name, final String driver, final Properties properties) { final PoolConfiguration config = build(TomEEPoolProperties.class, createProperties(name, properties)); final TomEEDataSource ds = new TomEEDataSource(config, name); recipes.put(ds, recipes.remove(config)); return ds; }
@Override protected void doDestroy(final CommonDataSource dataSource) throws Throwable { final org.apache.tomcat.jdbc.pool.DataSource ds = (org.apache.tomcat.jdbc.pool.DataSource) dataSource; if (ds instanceof TomEEDataSource) { ((TomEEDataSource) ds).internalJMXUnregister(); } ds.close(true); }
@Override public DataSource managed(final String name, final CommonDataSource ds) { final TransactionManager transactionManager = OpenEJB.getTransactionManager(); if (ds instanceof XADataSource) { return new ManagedXADataSource(ds, transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); } return new ManagedDataSource(DataSource.class.cast(ds), transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); }
public DbcpManagedDataSource(final String name, final CommonDataSource dataSource) { super(name); this.ds = dataSource; if (XADataSource.class.isInstance(dataSource)) { setXaDataSourceInstance(XADataSource.class.cast(ds)); } }
@Override public DataSource poolManagedWithRecovery(final String name, final XAResourceWrapper xaResourceWrapper, final String driver, final Properties properties) { final TransactionManager transactionManager = new TransactionManagerWrapper(OpenEJB.getTransactionManager(), name, xaResourceWrapper); final CommonDataSource ds = pool(name, driver, properties); if (ds instanceof XADataSource) { return new ManagedXADataSource(ds, transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); } return new ManagedDataSource(DataSource.class.cast(ds), transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); }
private void createANewDelegate() { final CommonDataSource old = delegate.get(); try { final ObjectRecipe recipe = new ObjectRecipe(DataSourceFactory.class.getName(), "create", FACTORY_ARGS); recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES); recipe.allow(Option.IGNORE_MISSING_PROPERTIES); recipe.allow(Option.NAMED_PARAMETERS); recipe.allow(Option.PRIVATE_PROPERTIES); recipe.setAllProperties(config.properties); recipe.setProperty("resettableHandler", resettableHandler); recipe.setProperty("flushableHandler", this); updateDataSource(CommonDataSource.class.cast(recipe.create())); } catch (final Exception e) { LOGGER.error("Can't recreate the datasource, keeping old one", e); return; } if (DataSourceFactory.knows(old)) { try { DataSourceFactory.destroy(old); } catch (final Throwable t) { //Ignore } } }
public void updateDataSource(final CommonDataSource ds) { // order is important, check DataSourceFactory CommonDataSource current = ds; while (Proxy.isProxyClass(current.getClass())) { final InvocationHandler handler = Proxy.getInvocationHandler(current); if (FlushableDataSourceHandler.class.isInstance(handler) || ResettableDataSourceHandler.class.isInstance(handler)) { current = DelegatableHandler.class.cast(handler).getDelegate(); } else { break; } } delegate.set(current); }
public ResettableDataSourceHandler(final CommonDataSource ds, final String value, final String methods) { this.delegate.set(ds); if (!"*".equals(methods)) { this.retryMethods.addAll(asList(methods == null ? new String[]{"getConnection", "getXAConnection"} : methods.split(" *, *"))); } final Runnable recreate = new Runnable() { @Override public void run() { try { Flushable.class.cast(delegate.get()).flush(); } catch (final IOException ioe) { LOGGER.error("Can't flush connection pool: " + ioe.getMessage()); } } }; RetryStrategy tmp; if (value.equals("true")) { tmp = new CountRetryStrategy(recreate, 1); } else if (value.startsWith("retry(") && value.endsWith(")")) { tmp = new CountRetryStrategy(recreate, Integer.parseInt(value.substring("retry(".length(), value.length() - 1))); } else { try { tmp = new CountRetryStrategy(recreate, Integer.parseInt(value.trim())); } catch (final NumberFormatException nfe) { try { tmp = RetryStrategy.class.cast(Thread.currentThread().getContextClassLoader().loadClass(value) .getConstructor(Runnable.class, String.class).newInstance(recreate, value)); } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) { throw new IllegalArgumentException("Unknown retry strategy: " + value, e); } } } strategy = tmp; }
public static void setUrl(final CommonDataSource dataSource, final String url) throws Exception { // TODO This is a big whole and we will need to rework this if (url.contains("jdbc:derby:")) { DataSourceHelper.setUrl(dataSource, url.replace("jdbc:derby:", ""), dataSource.getClass().getClassLoader(), "org.apache.derby.jdbc.EmbeddedDataSource", "setDatabaseName"); } else { DataSourceHelper.setUrl(dataSource, url, dataSource.getClass().getClassLoader(), "org.hsqldb.jdbc.JDBCDataSource", "setDatabase"); } }
@Override public DataSource managed(final String name, final CommonDataSource ds) { final TransactionManager transactionManager = OpenEJB.getTransactionManager(); if (XADataSource.class.isInstance(ds)) { return new ManagedXADataSource(XADataSource.class.cast(ds), transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); } return new ManagedDataSource(DataSource.class.cast(ds), transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); }
public ManagedConnection(final CommonDataSource ds, final TransactionManager txMgr, final TransactionSynchronizationRegistry txRegistry, final String user, final String password) { transactionManager = txMgr; registry = txRegistry; closed = false; key = new Key(ds, user, password); }
private Key(final CommonDataSource ds, final String user, final String pwd) { this.ds = ds; this.user = user; this.pwd = pwd; int result = ds.hashCode(); result = 31 * result + (user != null ? user.hashCode() : 0); result = 31 * result + (pwd != null ? pwd.hashCode() : 0); hash = result; }
public CommonDataSource create() { final TransactionManager transactionManager = OpenEJB.getTransactionManager(); CommonDataSource cds = findDelegate(); if (cds instanceof XADataSource) { cds = new ManagedXADataSource(cds, transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); } else { cds = new ManagedDataSource(DataSource.class.cast(cds), transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); } if (logSql) { cds = DataSourceFactory.makeItLogging(cds, logPackages); } return cds; }
public void setJtaDataSource(final CommonDataSource jtaDataSource) { if (XADataSource.class.isInstance(jtaDataSource)) { this.jtaDataSource = new DataSourceXADataSource( jtaDataSource, OpenEJB.getTransactionManager(), SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); } else { this.jtaDataSource = DataSource.class.cast(jtaDataSource); } }
public void setNonJtaDataSource(final CommonDataSource nonJtaDataSource) { if (XADataSource.class.isInstance(nonJtaDataSource)) { this.nonJtaDataSource = new DataSourceXADataSource( nonJtaDataSource, OpenEJB.getTransactionManager(), SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class)); } else { this.nonJtaDataSource = DataSource.class.cast(nonJtaDataSource); } }
@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); }
public CommonDataSource getWrappedObject() throws SQLException { if ( _embedded != null ) { return _embedded; } else if ( _netclient != null ) { return _netclient; } else if ( _ecpds != null ) { return _ecpds; } else if ( _exads != null ) { return _exads; } else if ( _ccpds != null ) { return _ccpds; } else if ( _cxads != null ) { return _cxads; } else { throw nothingWrapped(); } }
private boolean matchesDataSource(DecoratedDataSource decoratedCandidate, CommonDataSource dataSource) { return decoratedCandidate.getRealDataSource() == dataSource || decoratedCandidate.getDecoratingChain().stream() .map(DataSourceDecorationStage::getDataSource) .anyMatch(candidate -> candidate == dataSource); }