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;
}
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);
}
}
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();
}
}
Aggregations