Search in sources :

Example 1 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project hibernate-orm by hibernate.

the class ProxoolConnectionProviderTest method testPoolsClosed.

@Test
public void testPoolsClosed() {
    // zero-length-vararg used as parameter
    assertDefinedPools();
    StandardServiceRegistry serviceRegistry = buildServiceRegistry("pool-one");
    ConnectionProvider providerOne = serviceRegistry.getService(ConnectionProvider.class);
    assertDefinedPools("pool-one");
    StandardServiceRegistry serviceRegistryTwo = buildServiceRegistry("pool-two");
    ConnectionProvider providerTwo = serviceRegistryTwo.getService(ConnectionProvider.class);
    assertDefinedPools("pool-one", "pool-two");
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
    assertDefinedPools("pool-two");
    StandardServiceRegistryBuilder.destroy(serviceRegistryTwo);
    assertDefinedPools();
}
Also used : StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) ProxoolConnectionProvider(org.hibernate.proxool.internal.ProxoolConnectionProvider) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test)

Example 2 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project hibernate-orm by hibernate.

the class BaseTransactionIsolationConfigTest method testSettingIsolationAsName.

@Test
public void testSettingIsolationAsName() throws Exception {
    Properties properties = Environment.getProperties();
    augmentConfigurationSettings(properties);
    properties.put(AvailableSettings.ISOLATION, "TRANSACTION_SERIALIZABLE");
    ConnectionProvider provider = getConnectionProviderUnderTest();
    try {
        ((Configurable) provider).configure(properties);
        if (Startable.class.isInstance(provider)) {
            ((Startable) provider).start();
        }
        Connection connection = provider.getConnection();
        assertEquals(Connection.TRANSACTION_SERIALIZABLE, connection.getTransactionIsolation());
        provider.closeConnection(connection);
    } finally {
        ((Stoppable) provider).stop();
    }
}
Also used : Startable(org.hibernate.service.spi.Startable) Connection(java.sql.Connection) Stoppable(org.hibernate.service.spi.Stoppable) Configurable(org.hibernate.service.spi.Configurable) Properties(java.util.Properties) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test)

Example 3 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project spring-framework by spring-projects.

the class SessionFactoryUtils method getDataSource.

/**
	 * Determine the DataSource of the given SessionFactory.
	 * @param sessionFactory the SessionFactory to check
	 * @return the DataSource, or {@code null} if none found
	 * @see ConnectionProvider
	 */
public static DataSource getDataSource(SessionFactory sessionFactory) {
    Method getProperties = ClassUtils.getMethodIfAvailable(sessionFactory.getClass(), "getProperties");
    if (getProperties != null) {
        Map<?, ?> props = (Map<?, ?>) ReflectionUtils.invokeMethod(getProperties, sessionFactory);
        Object dataSourceValue = props.get(Environment.DATASOURCE);
        if (dataSourceValue instanceof DataSource) {
            return (DataSource) dataSourceValue;
        }
    }
    if (sessionFactory instanceof SessionFactoryImplementor) {
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
        try {
            ConnectionProvider cp = sfi.getServiceRegistry().getService(ConnectionProvider.class);
            if (cp != null) {
                return cp.unwrap(DataSource.class);
            }
        } catch (UnknownServiceException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("No ConnectionProvider found - cannot determine DataSource for SessionFactory: " + ex);
            }
        }
    }
    return null;
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) UnknownServiceException(org.hibernate.service.UnknownServiceException) Method(java.lang.reflect.Method) Map(java.util.Map) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)

Example 4 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project hibernate-orm by hibernate.

the class FooBarTest method testUserProvidedConnection.

@Test
@RequiresDialect(value = H2Dialect.class, comment = "this is more like a unit test")
public void testUserProvidedConnection() throws Exception {
    ConnectionProvider dcp = ConnectionProviderBuilder.buildConnectionProvider();
    Session s = sessionFactory().withOptions().connection(dcp.getConnection()).openSession();
    Transaction tx = s.beginTransaction();
    s.createQuery("from Fo").list();
    tx.commit();
    Connection c = s.disconnect();
    assertTrue(c != null);
    s.reconnect(c);
    tx = s.beginTransaction();
    s.createQuery("from Fo").list();
    tx.commit();
    c.close();
}
Also used : Transaction(org.hibernate.Transaction) Connection(java.sql.Connection) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 5 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project jbosstools-hibernate by jbosstools.

the class MockConnectionProviderTest method testGetConnection.

@Test
public void testGetConnection() throws Exception {
    assertNotNull(MockConnectionProvider.CONNECTION);
    assertNotNull(MockConnectionProvider.DATABASE_META_DATA);
    ConnectionProvider connectionProvider = new MockConnectionProvider();
    Connection connection = connectionProvider.getConnection();
    assertSame(MockConnectionProvider.CONNECTION, connection);
    assertSame(MockConnectionProvider.DATABASE_META_DATA, connection.getMetaData());
}
Also used : Connection(java.sql.Connection) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)24 Connection (java.sql.Connection)13 Test (org.junit.Test)11 Properties (java.util.Properties)5 Test (org.junit.jupiter.api.Test)5 SQLException (java.sql.SQLException)4 IOException (java.io.IOException)3 Map (java.util.Map)3 MetadataSources (org.hibernate.boot.MetadataSources)3 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)3 Configurable (org.hibernate.service.spi.Configurable)3 Startable (org.hibernate.service.spi.Startable)3 Stoppable (org.hibernate.service.spi.Stoppable)3 DdlTransactionIsolatorTestingImpl (org.hibernate.test.util.DdlTransactionIsolatorTestingImpl)3 TestForIssue (org.hibernate.testing.TestForIssue)3 Method (java.lang.reflect.Method)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2