@Test public void testSelectStatementWithStatementsCache() throws SQLException { Session session = HibernateTestUtils.getSessionFactoryWithStmtCache().openSession(); ConnectionProvider cp = ((SessionFactoryImplementor) session.getSessionFactory()) .getServiceRegistry().getService(ConnectionProvider.class); ViburDBCPDataSource ds = ((ViburDBCPConnectionProvider) cp).getDataSource(); ConcurrentMap<StatementMethod, StatementHolder> mockedStatementCache = mockStatementCache(ds); executeAndVerifySelectInSession(session); // resources/hibernate-with-stmt-cache.cfg.xml defines pool with 1 connection only, that's why // the second session will get and use the same underlying connection. session = HibernateTestUtils.getSessionFactoryWithStmtCache().openSession(); executeAndVerifySelectInSession(session); InOrder inOrder = inOrder(mockedStatementCache); inOrder.verify(mockedStatementCache).get(key1.capture()); inOrder.verify(mockedStatementCache).putIfAbsent(same(key1.getValue()), val1.capture()); inOrder.verify(mockedStatementCache).get(key2.capture()); assertEquals(1, mockedStatementCache.size()); assertTrue(mockedStatementCache.containsKey(key1.getValue())); assertEquals(key1.getValue(), key2.getValue()); assertEquals(AVAILABLE, val1.getValue().state().get()); }
@Override public ServiceRegistry buildServiceRegistry() { if (connectionProviderBuilder == null || configurationPovider == null) { throw new IllegalArgumentException(); } LOG.debug("Creating service registry"); BootstrapServiceRegistry bootstrapServiceRegistry = buildBootstrapServiceRegistry(); org.hibernate.service.ServiceRegistryBuilder builder = new org.hibernate.service.ServiceRegistryBuilder( bootstrapServiceRegistry).addService(ConnectionProvider.class, connectionProviderBuilder.buildConnectionProvider()); if (sessionObserver != null) { builder.addService(SessionObserver.class, sessionObserver); } builder.applySettings(configurationPovider.getConfiguration().getProperties()); return builder.buildServiceRegistry(); }
@Override @SuppressWarnings( {"unchecked"}) public <T> T unwrap(Class<T> unwrapType) { if ( ConnectionProvider.class.equals( unwrapType ) || CloudSqlConnectionProvider.class.isAssignableFrom( unwrapType ) ) { return (T) this; } else { throw new UnknownUnwrapTypeException( unwrapType ); } }
@Override public ConnectionProvider buildConnectionProvider() { if (this.dataSourceProvider == null) { throw new IllegalStateException(); } LOG.debug("Building new Connection Provider using datasourceProvider"); DatasourceConnectionProviderImpl connectionProvider = new DatasourceConnectionProviderImpl(); connectionProvider.setDataSource(dataSourceProvider.getDataSource()); connectionProvider.configure(Collections.emptyMap()); return connectionProvider; }
@Override public synchronized SessionFactory buildSessionFactory() { try { LOG.debug("Building new session factory"); SessionFactory real = configurationProvider.getConfiguration().buildSessionFactory( this.serviceRegistryBuilder.buildServiceRegistry()); LOG.debug("Session factory created, creating new Transaction Manager to handle transactions"); HibernateTransactionManager txManager = new HibernateTransactionManager(); txManager.setDataSource(((DatasourceConnectionProviderImpl) ((SessionFactoryImplementor) real).getServiceRegistry() .getService(ConnectionProvider.class)).getDataSource()); txManager.setSessionFactory(proxySessionFactory); txManager.setValidateExistingTransaction(true); txManager.afterPropertiesSet(); // The anotations at this point uses the new transaction manager AnnotationTransactionAspect txAspect = AnnotationTransactionAspect.aspectOf(); txAspect.setTransactionManager(txManager); LOG.debug("New transaction manager established to AnnotationTransactionAspect, now the annotations used the new Transaction Manager"); return real; } catch (RuntimeException ex) { LOG.error("Runtime exception when trying to create session factory.", ex); throw ex; } }
/** * 取得hibernate的connection对象 */ protected Connection getConnection() throws SQLException { if (sessionFactory instanceof SessionFactoryImpl) { SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory; ConnectionProvider provider = sessionFactoryImpl.getServiceRegistry().getService(ConnectionProvider.class); if(provider != null) return provider.getConnection(); } return null; }
/** * Determine the DataSource of the given SessionFactory. * @param sessionFactory the SessionFactory to check * @return the DataSource, or {@code null} if none found * @see org.hibernate.engine.spi.SessionFactoryImplementor#getConnectionProvider */ public static DataSource getDataSource(SessionFactory sessionFactory) { if (sessionFactory instanceof SessionFactoryImplementor) { ConnectionProvider cp = ((SessionFactoryImplementor) sessionFactory).getConnectionProvider(); return cp.unwrap(DataSource.class); } return null; }
private JdbcConnectionAccess buildLocalConnectionAccess() { return new JdbcConnectionAccess() { @Override public Connection obtainConnection() throws SQLException { return settings.getMultiTenancyStrategy() == MultiTenancyStrategy.NONE ? serviceRegistry .getService(ConnectionProvider.class).getConnection() : serviceRegistry.getService( MultiTenantConnectionProvider.class) .getAnyConnection(); } @Override public void releaseConnection(Connection connection) throws SQLException { if (settings.getMultiTenancyStrategy() == MultiTenancyStrategy.NONE) { serviceRegistry.getService(ConnectionProvider.class) .closeConnection(connection); } else { serviceRegistry.getService( MultiTenantConnectionProvider.class) .releaseAnyConnection(connection); } } @Override public boolean supportsAggressiveRelease() { return false; } }; }
@Override public void prepare(boolean needsAutoCommit) throws SQLException { serviceRegistry = createServiceRegistry(cfgProperties); connection = serviceRegistry.getService(ConnectionProvider.class).getConnection(); if (needsAutoCommit && !connection.getAutoCommit()) { connection.commit(); connection.setAutoCommit(true); } }
private void releaseConnection() throws SQLException { if (connection != null) { try { new SqlExceptionHelper().logAndClearWarnings(connection); } finally { try { serviceRegistry.getService(ConnectionProvider.class).closeConnection(connection); } finally { connection = null; } } } }
@Override public boolean isUnwrappableAs(Class unwrapType) { return ConnectionProvider.class.equals(unwrapType) || ViburDBCPConnectionProvider.class.isAssignableFrom(unwrapType); }
@Override public boolean isUnwrappableAs(Class unwrapType) { return ConnectionProvider.class.equals( unwrapType ) || CloudSqlConnectionProvider.class.isAssignableFrom( unwrapType ); }
public ConnectionProvider getConnectionProvider() { return jdbcServices.getConnectionProvider(); }
public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider) { this.provider = provider; }
/** {@inheritDoc} */ @Nullable @Override protected ServiceRegistryBuilder registryBuilder() { ServiceRegistryBuilder builder = new ServiceRegistryBuilder(); 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.addService(TransactionFactory.class, new JtaTransactionFactory()); return builder; }
public ConnectionProvider buildConnectionProvider();