Search in sources :

Example 6 with ConnectionFactoryConfiguration

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

the class StringStoreWithPooledConnectionFunctionalTest method getConnectionFactory.

@Override
public ConnectionFactory getConnectionFactory(JdbcStringBasedStoreConfigurationBuilder storeBuilder) {
    ConnectionFactoryConfiguration connectionFactoryConfiguration;
    if (customFactoryConfiguration != null) {
        connectionFactoryConfiguration = storeBuilder.connectionPool().read(customFactoryConfiguration).create();
    } else {
        connectionFactoryConfiguration = configureUniqueConnectionFactory(storeBuilder).create();
    }
    final ConnectionFactory connectionFactory = ConnectionFactory.getConnectionFactory(PooledConnectionFactory.class);
    connectionFactory.start(connectionFactoryConfiguration, connectionFactory.getClass().getClassLoader());
    return connectionFactory;
}
Also used : ConnectionFactory(org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory) UnitTestDatabaseManager.configureUniqueConnectionFactory(org.infinispan.persistence.jdbc.UnitTestDatabaseManager.configureUniqueConnectionFactory) PooledConnectionFactory(org.infinispan.persistence.jdbc.common.impl.connectionfactory.PooledConnectionFactory) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration) PooledConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration)

Example 7 with ConnectionFactoryConfiguration

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

the class PooledConnectionFactory method start.

@Override
public void start(ConnectionFactoryConfiguration config, ClassLoader classLoader) throws PersistenceException {
    PooledConnectionFactoryConfiguration poolConfig;
    if (config instanceof PooledConnectionFactoryConfiguration) {
        poolConfig = (PooledConnectionFactoryConfiguration) config;
    } else {
        throw new PersistenceException("ConnectionFactoryConfiguration passed in must be an instance of " + "PooledConnectionFactoryConfiguration");
    }
    try {
        String propsFile = poolConfig.propertyFile();
        if (propsFile != null) {
            dataSource = AgroalDataSource.from(new AgroalPropertiesReader(PROPERTIES_PREFIX).readProperties(propsFile));
        } else {
            Class<?> driverClass = Util.loadClass(poolConfig.driverClass(), null);
            // Default Agroal configuration with metrics disabled
            String password = poolConfig.password() != null ? poolConfig.password() : "";
            AgroalDataSourceConfigurationSupplier configuration = new AgroalDataSourceConfigurationSupplier().connectionPoolConfiguration(cp -> cp.maxSize(10).acquisitionTimeout(Duration.ofSeconds(30)).connectionFactoryConfiguration(cf -> cf.jdbcUrl(poolConfig.connectionUrl()).connectionProviderClass(driverClass).jdbcTransactionIsolation(AgroalConnectionFactoryConfiguration.TransactionIsolation.UNDEFINED).principal(new NamePrincipal(poolConfig.username())).credential(new SimplePassword(password))));
            dataSource = AgroalDataSource.from(configuration);
        }
    } catch (Exception e) {
        throw new PersistenceException("Failed to create a AgroalDataSource", e);
    }
}
Also used : JdbcUtil(org.infinispan.persistence.jdbc.common.JdbcUtil) Connection(java.sql.Connection) LogFactory(org.infinispan.util.logging.LogFactory) Util(org.infinispan.commons.util.Util) PooledConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration) AgroalPropertiesReader(io.agroal.api.configuration.supplier.AgroalPropertiesReader) NamePrincipal(io.agroal.api.security.NamePrincipal) Log(org.infinispan.persistence.jdbc.common.logging.Log) SQLException(java.sql.SQLException) ConnectionFactory(org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration) Duration(java.time.Duration) AgroalDataSourceConfigurationSupplier(io.agroal.api.configuration.supplier.AgroalDataSourceConfigurationSupplier) AgroalConnectionFactoryConfiguration(io.agroal.api.configuration.AgroalConnectionFactoryConfiguration) SimplePassword(io.agroal.api.security.SimplePassword) AgroalDataSource(io.agroal.api.AgroalDataSource) PersistenceException(org.infinispan.persistence.spi.PersistenceException) PooledConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration) AgroalDataSourceConfigurationSupplier(io.agroal.api.configuration.supplier.AgroalDataSourceConfigurationSupplier) NamePrincipal(io.agroal.api.security.NamePrincipal) PersistenceException(org.infinispan.persistence.spi.PersistenceException) AgroalPropertiesReader(io.agroal.api.configuration.supplier.AgroalPropertiesReader) SimplePassword(io.agroal.api.security.SimplePassword) SQLException(java.sql.SQLException) PersistenceException(org.infinispan.persistence.spi.PersistenceException)

Example 8 with ConnectionFactoryConfiguration

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

the class AbstractSQLStoreFunctionalTest method createTable.

protected void createTable(String cacheName, String tableName, ConnectionFactoryConfigurationBuilder<ConnectionFactoryConfiguration> builder) {
    String tableCreation;
    String upperCaseCacheName = cacheName.toUpperCase();
    if (cacheName.equalsIgnoreCase("testPreloadStoredAsBinary")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn VARCHAR(255) NOT NULL, " + "NAME VARCHAR(255) NOT NULL, " + "street VARCHAR(255), " + "city VARCHAR(255), " + "zip INT, " + "picture " + binaryType() + ", " + "accepted_tos " + booleanType() + ", " + "sex VARCHAR(255), " + "birthdate " + dateTimeType() + ", " + "PRIMARY KEY (keycolumn))";
    } else if (cacheName.equalsIgnoreCase("testStoreByteArrays")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn " + binaryType() + " NOT NULL, " + "value1 " + binaryType() + " NOT NULL, " + "PRIMARY KEY (keycolumn))";
    } else if (upperCaseCacheName.startsWith("TESTDBHASMOREVALUECOLUMNS")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn VARCHAR(255) NOT NULL, " + "NAME VARCHAR(255) NOT NULL, " + "street VARCHAR(255), " + "city VARCHAR(255), " + "zip INT, " + "picture " + binaryType() + ", " + "sex VARCHAR(255), " + "birthdate " + dateTimeType() + ", " + "value2 VARCHAR(255), " + "value3 VARCHAR(255), " + "PRIMARY KEY (keycolumn))";
    } else if (upperCaseCacheName.startsWith("TESTDBHASMOREKEYCOLUMNS")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + // The name of the field for the Key schema is "value"
        "value VARCHAR(255) NOT NULL, " + "keycolumn2 VARCHAR(255) NOT NULL," + "value1 VARCHAR(255) NOT NULL, " + "PRIMARY KEY (value, keycolumn2))";
    } else if (upperCaseCacheName.startsWith("TESTDBHASLESSVALUECOLUMNS")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn VARCHAR(255) NOT NULL, " + "NAME VARCHAR(255) NOT NULL, " + "street VARCHAR(255), " + "PRIMARY KEY (keycolumn))";
    } else if (upperCaseCacheName.startsWith("TESTEMBEDDED")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "NAME VARCHAR(255) NOT NULL, " + "street VARCHAR(255), " + "city VARCHAR(255), " + "zip INT, " + "picture " + binaryType() + ", " + "sex VARCHAR(255), " + "birthdate " + dateTimeType() + ", " + "PRIMARY KEY (name))";
    } else if (upperCaseCacheName.startsWith("TESTENUMFORVALUE")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "NAME VARCHAR(255) NOT NULL, " + "sex VARCHAR(255), " + "PRIMARY KEY (name))";
    } else if (upperCaseCacheName.startsWith("TESTENUMFORKEY")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "sex VARCHAR(255) NOT NULL, " + "name VARCHAR(255), " + "PRIMARY KEY (sex))";
    } else {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn VARCHAR(255) NOT NULL, " + "value1 VARCHAR(255) NOT NULL, " + "PRIMARY KEY (keycolumn))";
    }
    ConnectionFactoryConfiguration config = builder.create();
    ConnectionFactory factory = ConnectionFactory.getConnectionFactory(config.connectionFactoryClass());
    factory.start(config, getClass().getClassLoader());
    Connection connection = null;
    try {
        connection = factory.getConnection();
        String modifiedTableName = tableToSearch(tableName);
        try (ResultSet rs = connection.getMetaData().getTables(null, null, modifiedTableName, new String[] { "TABLE" })) {
            if (!rs.next()) {
                try (Statement stmt = connection.createStatement()) {
                    log.debugf("Table: %s doesn't exist, creating via %s%n", modifiedTableName, tableCreation);
                    stmt.execute(tableCreation);
                }
            }
        }
    } catch (SQLException t) {
        throw new AssertionError(t);
    } finally {
        factory.releaseConnection(connection);
        factory.stop();
    }
}
Also used : ConnectionFactory(org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(util.JdbcConnection) ResultSet(java.sql.ResultSet) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration)

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