Search in sources :

Example 1 with ConnectionFactoryConfiguration

use of org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration in project infinispan by infinispan.

the class QueriesJdbcJoinTest method createTables.

private void createTables(ConnectionFactoryConfigurationBuilder<ConnectionFactoryConfiguration> builder, boolean idJoin) {
    ConnectionFactoryConfiguration config = builder.create();
    FACTORY = ConnectionFactory.getConnectionFactory(config.connectionFactoryClass());
    FACTORY.start(config, getClass().getClassLoader());
    Connection connection = null;
    try {
        connection = FACTORY.getConnection();
        try (Statement stmt = connection.createStatement()) {
            String tableCreation = "CREATE TABLE " + TABLE1_NAME + " (" + "name VARCHAR(255) NOT NULL, " + (idJoin ? "address INT, " : "") + "picture VARBINARY(255), " + "sex VARCHAR(255), " + "birthdate TIMESTAMP, " + "accepted_tos boolean, " + "notused VARCHAR(255), " + "PRIMARY KEY (NAME))";
            stmt.execute(tableCreation);
            tableCreation = "create TABLE " + TABLE2_NAME + " (" + (idJoin ? "id INT NOT NULL, " : "name VARCHAR(255) NOT NULL, ") + "street VARCHAR(255), " + "city VARCHAR(255), " + "zip INT, " + "PRIMARY KEY (" + (idJoin ? "id" : "name") + "))";
            stmt.execute(tableCreation);
        }
    } catch (SQLException t) {
        throw new AssertionError(t);
    } finally {
        FACTORY.releaseConnection(connection);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration)

Example 2 with ConnectionFactoryConfiguration

use of org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration in project infinispan by infinispan.

the class TableManagerTest method createConnection.

@BeforeClass
public void createConnection() throws Exception {
    JdbcStringBasedStoreConfigurationBuilder storeBuilder = TestCacheManagerFactory.getDefaultCacheConfiguration(false).persistence().addStore(JdbcStringBasedStoreConfigurationBuilder.class);
    UnitTestDatabaseManager.setDialect(storeBuilder);
    UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table());
    ConnectionFactoryConfiguration factoryConfiguration = UnitTestDatabaseManager.configureUniqueConnectionFactory(storeBuilder).create();
    if (factoryConfiguration instanceof SimpleConnectionFactoryConfiguration) {
        SimpleConnectionFactoryConfiguration simpleConfiguration = (SimpleConnectionFactoryConfiguration) factoryConfiguration;
        connectionFactory = ConnectionFactory.getConnectionFactory(SimpleConnectionFactory.class);
        connectionFactory.start(simpleConfiguration, connectionFactory.getClass().getClassLoader());
        connection = connectionFactory.getConnection();
    } else if (factoryConfiguration instanceof PooledConnectionFactoryConfiguration) {
        PooledConnectionFactoryConfiguration pooledConfiguration = (PooledConnectionFactoryConfiguration) factoryConfiguration;
        connectionFactory = ConnectionFactory.getConnectionFactory(PooledConnectionFactory.class);
        connectionFactory.start(pooledConfiguration, connectionFactory.getClass().getClassLoader());
        connection = connectionFactory.getConnection();
    }
    Cache<?, ?> cache = mock(Cache.class);
    when(cache.getCacheConfiguration()).thenReturn(new ConfigurationBuilder().build());
    JdbcStringBasedStoreConfiguration config = storeBuilder.create();
    GlobalConfiguration globalConfiguration = mock(GlobalConfiguration.class);
    when(globalConfiguration.classLoader()).thenReturn(getClass().getClassLoader());
    ctx = new DummyInitializationContext(config, cache, new TestObjectStreamMarshaller(), null, null, null, globalConfiguration, null, null, null);
    tableManager = TableManagerFactory.getManager(ctx, connectionFactory, config, "aName");
}
Also used : SimpleConnectionFactory(org.infinispan.persistence.jdbc.common.impl.connectionfactory.SimpleConnectionFactory) DummyInitializationContext(org.infinispan.persistence.DummyInitializationContext) PooledConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) JdbcStringBasedStoreConfigurationBuilder(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfigurationBuilder) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) JdbcStringBasedStoreConfiguration(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfiguration) JdbcStringBasedStoreConfigurationBuilder(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfigurationBuilder) SimpleConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.SimpleConnectionFactoryConfiguration) TestObjectStreamMarshaller(org.infinispan.marshall.TestObjectStreamMarshaller) PooledConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration) SimpleConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.SimpleConnectionFactoryConfiguration) BeforeClass(org.testng.annotations.BeforeClass)

Example 3 with ConnectionFactoryConfiguration

use of org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration in project infinispan by infinispan.

the class PooledConnectionFactoryTest method testValuesNoOverrides.

@Test
public void testValuesNoOverrides() throws Exception {
    storeBuilder = TestCacheManagerFactory.getDefaultCacheConfiguration(false).persistence().addStore(JdbcStringBasedStoreConfigurationBuilder.class);
    if (customFactoryConfiguration != null) {
        factoryBuilder = storeBuilder.connectionPool().read(customFactoryConfiguration);
    } else {
        // We load agroal.properties to enable metrics and ensure that property file loading works as expected
        factoryBuilder = storeBuilder.connectionPool().propertyFile("src/test/resources/configs/agroal.properties");
    }
    ConnectionFactoryConfiguration factoryConfiguration = factoryBuilder.create();
    factory.start(factoryConfiguration, Thread.currentThread().getContextClassLoader());
    int hardcodedMaxPoolSize = factory.getMaxPoolSize();
    assert hardcodedMaxPoolSize == 20;
    Set<Connection> connections = new HashSet<>();
    for (int i = 0; i < hardcodedMaxPoolSize; i++) {
        connections.add(factory.getConnection());
    }
    assert connections.size() == hardcodedMaxPoolSize;
    assert factory.getActiveConnections() == hardcodedMaxPoolSize;
    for (Connection conn : connections) {
        conn.close();
    }
    long start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < 2000) {
        if (factory.getActiveConnections() == 0)
            break;
    }
    // this must happen eventually
    assert factory.getActiveConnections() == 0;
}
Also used : Connection(java.sql.Connection) JdbcStringBasedStoreConfigurationBuilder(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfigurationBuilder) PooledConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with ConnectionFactoryConfiguration

use of org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration in project infinispan by infinispan.

the class PooledConnectionFactoryTest method testNoDriverClassFound.

@Test(expectedExceptions = PersistenceException.class)
public void testNoDriverClassFound() {
    storeBuilder = TestCacheManagerFactory.getDefaultCacheConfiguration(false).persistence().addStore(JdbcStringBasedStoreConfigurationBuilder.class);
    factoryBuilder = UnitTestDatabaseManager.configureBrokenConnectionFactory(storeBuilder);
    ConnectionFactoryConfiguration factoryConfiguration = factoryBuilder.create();
    factory.start(factoryConfiguration, Thread.currentThread().getContextClassLoader());
}
Also used : JdbcStringBasedStoreConfigurationBuilder(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfigurationBuilder) PooledConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration) Test(org.testng.annotations.Test)

Example 5 with ConnectionFactoryConfiguration

use of org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration in project infinispan by infinispan.

the class TableManagerTest method testConnectionLeakGuessDialect.

public void testConnectionLeakGuessDialect() {
    JdbcStringBasedStoreConfigurationBuilder storeBuilder = TestCacheManagerFactory.getDefaultCacheConfiguration(false).persistence().addStore(JdbcStringBasedStoreConfigurationBuilder.class);
    UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table());
    PooledConnectionFactory connectionFactory = new PooledConnectionFactory();
    ConnectionFactoryConfiguration config = UnitTestDatabaseManager.configureUniqueConnectionFactory(storeBuilder).create();
    connectionFactory.start(config, Thread.currentThread().getContextClassLoader());
    // JdbcStringBasedStoreConfiguration defaults to null dialect, so dialect and versions must be guessed
    TableManager tableManager = TableManagerFactory.getManager(ctx, connectionFactory, storeBuilder.create(), "GuessDialect");
    tableManager.start();
    UnitTestDatabaseManager.verifyConnectionLeaks(connectionFactory);
    tableManager.stop();
    connectionFactory.stop();
}
Also used : PooledConnectionFactory(org.infinispan.persistence.jdbc.common.impl.connectionfactory.PooledConnectionFactory) TableManager(org.infinispan.persistence.jdbc.impl.table.TableManager) JdbcStringBasedStoreConfigurationBuilder(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfigurationBuilder) PooledConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration) SimpleConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.SimpleConnectionFactoryConfiguration)

Aggregations

ConnectionFactoryConfiguration (org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration)8 PooledConnectionFactoryConfiguration (org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration)6 Connection (java.sql.Connection)4 JdbcStringBasedStoreConfigurationBuilder (org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfigurationBuilder)4 SQLException (java.sql.SQLException)3 ConnectionFactory (org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory)3 Statement (java.sql.Statement)2 SimpleConnectionFactoryConfiguration (org.infinispan.persistence.jdbc.common.configuration.SimpleConnectionFactoryConfiguration)2 PooledConnectionFactory (org.infinispan.persistence.jdbc.common.impl.connectionfactory.PooledConnectionFactory)2 Test (org.testng.annotations.Test)2 AgroalDataSource (io.agroal.api.AgroalDataSource)1 AgroalConnectionFactoryConfiguration (io.agroal.api.configuration.AgroalConnectionFactoryConfiguration)1 AgroalDataSourceConfigurationSupplier (io.agroal.api.configuration.supplier.AgroalDataSourceConfigurationSupplier)1 AgroalPropertiesReader (io.agroal.api.configuration.supplier.AgroalPropertiesReader)1 NamePrincipal (io.agroal.api.security.NamePrincipal)1 SimplePassword (io.agroal.api.security.SimplePassword)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Duration (java.time.Duration)1 HashSet (java.util.HashSet)1