public GrailsHibernateTemplate(SessionFactory sessionFactory) { Assert.notNull(sessionFactory, "Property 'sessionFactory' is required"); this.sessionFactory = sessionFactory; ConnectionProvider connectionProvider = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry().getService(ConnectionProvider.class); if(connectionProvider instanceof DatasourceConnectionProviderImpl) { this.dataSource = ((DatasourceConnectionProviderImpl) connectionProvider).getDataSource(); if(dataSource instanceof TransactionAwareDataSourceProxy) { this.dataSource = ((TransactionAwareDataSourceProxy) dataSource).getTargetDataSource(); } jdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); } else { // must be in unit test mode, setup default translator SQLErrorCodeSQLExceptionTranslator sqlErrorCodeSQLExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(); sqlErrorCodeSQLExceptionTranslator.setDatabaseProductName("H2"); jdbcExceptionTranslator = sqlErrorCodeSQLExceptionTranslator; } }
private ConnectionProvider buildConnectionProvider(final DataSource dataSource, final Map<String, String> properties) { final DatasourceConnectionProviderImpl connectionProvider = new DatasourceConnectionProviderImpl(); connectionProvider.setDataSource(dataSource); connectionProvider.configure(properties); return connectionProvider; }
@Override @SuppressWarnings({ "unchecked" }) public <T> T unwrap(Class<T> unwrapType) { if (ConnectionProvider.class.equals(unwrapType) || DatasourceConnectionProviderImpl.class.isAssignableFrom(unwrapType)) { return (T) this; } else if (DataSource.class.isAssignableFrom(unwrapType)) { return (T) this.dataSource; } else { throw new UnknownUnwrapTypeException(unwrapType); } }
public MultiTenantConnectionProviderImpl(MultiTenantDataSource dataSource) { this.dataSourceConnectionProvider = new DatasourceConnectionProviderImpl(); this.dataSourceConnectionProvider.setDataSource(dataSource); // this triggers the 'available' flag this.dataSourceConnectionProvider.configure(new HashMap<Object, Object>()); }
protected DatasourceConnectionProviderImpl getDatasourceConnectionProvider() { return this.dataSourceConnectionProvider; }
@Override public boolean isUnwrappableAs(Class unwrapType) { return ConnectionProvider.class.equals(unwrapType) || DatasourceConnectionProviderImpl.class.isAssignableFrom(unwrapType) || DataSource.class.isAssignableFrom(unwrapType); }
/** {@inheritDoc} */ @Nullable @Override protected StandardServiceRegistryBuilder registryBuilder() { StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); DatasourceConnectionProviderImpl connProvider = new DatasourceConnectionProviderImpl(); BasicManagedDataSource dataSrc = new BasicManagedDataSource(); // JTA-aware data source. dataSrc.setTransactionManager(jotm.getTransactionManager()); dataSrc.setDefaultAutoCommit(false); JdbcDataSource h2DataSrc = new JdbcDataSource(); h2DataSrc.setURL(CONNECTION_URL); dataSrc.setXaDataSourceInstance(h2DataSrc); connProvider.setDataSource(dataSrc); connProvider.configure(Collections.emptyMap()); builder.addService(ConnectionProvider.class, connProvider); builder.addService(JtaPlatform.class, new TestJtaPlatform()); builder.applySetting(Environment.TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class.getName()); return builder; }
/** * Builds a {@link ConnectionProvider} * @param dataSource * the datasource * @param properties * the connection properties * @return {@link ConnectionProvider} */ private ConnectionProvider buildConnectionProvider(DataSource dataSource, Map<String, String> properties) { final DatasourceConnectionProviderImpl connectionProvider = new DatasourceConnectionProviderImpl(); connectionProvider.setDataSource(dataSource); connectionProvider.configure(properties); return connectionProvider; }