Java Code Examples for org.hibernate.service.jdbc.connections.spi.ConnectionProvider

The following examples show how to use org.hibernate.service.jdbc.connections.spi.ConnectionProvider. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: snakerflow   Source File: Hibernate4Access.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 取得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;
}
 
Example 2
/** {@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;
}