use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class LdapOperationServiceImpl method deleteRecursivelyImpl.
protected boolean deleteRecursivelyImpl(String dn) {
try {
final DeleteRequest deleteRequest = new DeleteRequest(dn);
deleteRequest.addControl(new SubtreeDeleteRequestControl());
LDAPResult result = getConnectionPool().delete(deleteRequest);
return ResultCode.SUCCESS.equals(result.getResultCode());
} catch (Exception ex) {
throw new ConnectionException("Failed to delete entry", ex);
}
}
use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class SpannerConnectionProvider method loadTableMetaData.
private void loadTableMetaData() {
LOG.info("Scanning DB metadata...");
long takes = System.currentTimeMillis();
try (ResultSet resultSet = executeQuery(QUERY_PARENT_TABLE)) {
if (resultSet.next()) {
int tableNameIdx = resultSet.getColumnIndex("TABLE_NAME");
int parentTableNameIdx = resultSet.getColumnIndex("PARENT_TABLE_NAME");
do {
String parentTableName = resultSet.getString(parentTableNameIdx);
String tableName = resultSet.getString(tableNameIdx);
Set<String> childAttributes;
if (tableChildAttributesMap.containsKey(parentTableName)) {
childAttributes = tableChildAttributesMap.get(parentTableName);
} else {
childAttributes = new HashSet<>();
tableChildAttributesMap.put(parentTableName, childAttributes);
}
if (tableName.startsWith(parentTableName + "_")) {
tableName = tableName.substring(parentTableName.length() + 1);
}
childAttributes.add(tableName);
} while (resultSet.next());
}
} catch (SpannerException ex) {
throw new ConnectionException("Failed to get database metadata", ex);
}
LOG.debug("Build child attributes map: '{}'.", tableChildAttributesMap);
HashMap<String, Type> typeMap = buildSpannerTypesMap();
try (ResultSet resultSet = executeQuery(QUERY_TABLE_SCHEMA)) {
if (resultSet.next()) {
int tableNameIdx = resultSet.getColumnIndex("TABLE_NAME");
int columnNameIdx = resultSet.getColumnIndex("COLUMN_NAME");
int spannerTypeIdx = resultSet.getColumnIndex("SPANNER_TYPE");
int isNullableIdx = resultSet.getColumnIndex("IS_NULLABLE");
do {
String tableName = resultSet.getString(tableNameIdx);
String columnName = resultSet.getString(columnNameIdx);
String spannerType = resultSet.getString(spannerTypeIdx);
String isNullable = resultSet.getString(isNullableIdx);
// Load table schema
Map<String, StructField> tableColumns;
if (tableColumnsMap.containsKey(tableName)) {
tableColumns = tableColumnsMap.get(tableName);
} else {
tableColumns = new HashMap<>();
tableColumnsMap.put(tableName, tableColumns);
}
String comparebleType = toComparableType(spannerType);
Type type = typeMap.get(comparebleType);
if (type == null) {
throw new ConnectionException(String.format("Failed to parse SPANNER_TYPE: '%s'", spannerType));
}
tableColumns.put(columnName.toLowerCase(), StructField.of(columnName, type));
// Check if column nullable
Set<String> nullableColumns;
if (tableNullableColumnsSet.containsKey(tableName)) {
nullableColumns = tableNullableColumnsSet.get(tableName);
} else {
nullableColumns = new HashSet<>();
tableNullableColumnsSet.put(tableName, nullableColumns);
}
boolean nullable = "yes".equalsIgnoreCase(isNullable);
if (nullable) {
nullableColumns.add(columnName.toLowerCase());
}
} while (resultSet.next());
}
} catch (SpannerException ex) {
throw new ConnectionException("Failed to get database metadata", ex);
}
LOG.debug("Build table columns map: '{}'.", tableColumnsMap);
takes = System.currentTimeMillis() - takes;
LOG.info("Metadata scan finisehd in {} milliseconds", takes);
}
use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class SpannerConnectionProvider method openWithWaitImpl.
private void openWithWaitImpl() throws Exception {
long connectionMaxWaitTimeMillis = StringHelper.toLong(props.getProperty("connection.client.create-max-wait-time-millis"), 30 * 1000L);
LOG.debug("Using connection timeout: '{}'", connectionMaxWaitTimeMillis);
Exception lastException = null;
int attempt = 0;
long currentTime = System.currentTimeMillis();
long maxWaitTime = currentTime + connectionMaxWaitTimeMillis;
do {
attempt++;
if (attempt > 0) {
LOG.info("Attempting to create client connection: '{}'", attempt);
}
try {
open();
if (isConnected()) {
break;
} else {
LOG.info("Failed to connect to Spanner");
destroy();
throw new ConnectionException("Failed to create client connection");
}
} catch (Exception ex) {
lastException = ex;
}
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
LOG.error("Exception happened in sleep", ex);
return;
}
currentTime = System.currentTimeMillis();
} while (maxWaitTime > currentTime);
if (lastException != null) {
throw lastException;
}
}
use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class SqlConnectionProvider method init.
protected void init() throws Exception {
if (!props.containsKey("db.schema.name")) {
throw new ConfigurationException("Property 'db.schema.name' is mandatory!");
}
this.schemaName = props.getProperty("db.schema.name");
if (!props.containsKey("connection.uri")) {
throw new ConfigurationException("Property 'connection.uri' is mandatory!");
}
this.connectionUri = props.getProperty("connection.uri");
Properties filteredDriverProperties = PropertiesHelper.findProperties(props, DRIVER_PROPERTIES_PREFIX, ".");
this.connectionProperties = new Properties();
for (Entry<Object, Object> driverPropertyEntry : filteredDriverProperties.entrySet()) {
String key = StringHelper.toString(driverPropertyEntry.getKey()).substring(DRIVER_PROPERTIES_PREFIX.length() + 1);
String value = StringHelper.toString(driverPropertyEntry.getValue());
connectionProperties.put(key, value);
}
String userName = props.getProperty("auth.userName");
String userPassword = props.getProperty("auth.userPassword");
connectionProperties.setProperty("user", userName);
connectionProperties.setProperty("password", userPassword);
this.objectPoolConfig = new GenericObjectPoolConfig<>();
Integer cpMaxTotal = StringHelper.toInteger(props.getProperty("connection.pool.max-total"), null);
if (cpMaxTotal != null) {
objectPoolConfig.setMaxTotal(cpMaxTotal);
}
Integer cpMaxIdle = StringHelper.toInteger(props.getProperty("connection.pool.max-idle"), null);
if (cpMaxIdle != null) {
objectPoolConfig.setMaxIdle(cpMaxIdle);
}
Integer cpMinIdle = StringHelper.toInteger(props.getProperty("connection.pool.min-idle"), null);
if (cpMinIdle != null) {
objectPoolConfig.setMinIdle(cpMinIdle);
}
Integer cpMaxWaitTimeMillis = StringHelper.toInteger(props.getProperty("connection.pool.max-wait-time-millis"), null);
if (cpMaxWaitTimeMillis != null) {
objectPoolConfig.setMaxWaitMillis(cpMaxWaitTimeMillis);
}
Integer cpMinEvictableIdleTimeMillis = StringHelper.toInteger(props.getProperty("connection.pool.min-evictable-idle-time-millis"), null);
if (cpMaxWaitTimeMillis != null) {
objectPoolConfig.setMinEvictableIdleTimeMillis(cpMinEvictableIdleTimeMillis);
}
openWithWaitImpl();
LOG.info("Created connection pool");
if (props.containsKey("password.encryption.method")) {
this.passwordEncryptionMethod = PasswordEncryptionMethod.getMethod(props.getProperty("password.encryption.method"));
} else {
this.passwordEncryptionMethod = PasswordEncryptionMethod.HASH_METHOD_SHA256;
}
this.binaryAttributes = new ArrayList<String>();
if (props.containsKey("binaryAttributes")) {
String[] binaryAttrs = StringHelper.split(props.get("binaryAttributes").toString().toLowerCase(), ",");
this.binaryAttributes.addAll(Arrays.asList(binaryAttrs));
}
LOG.debug("Using next binary attributes: '{}'", binaryAttributes);
this.certificateAttributes = new ArrayList<String>();
if (props.containsKey("certificateAttributes")) {
String[] binaryAttrs = StringHelper.split(props.get("certificateAttributes").toString().toLowerCase(), ",");
this.certificateAttributes.addAll(Arrays.asList(binaryAttrs));
}
LOG.debug("Using next binary certificateAttributes: '{}'", certificateAttributes);
try (Connection con = this.poolingDataSource.getConnection()) {
DatabaseMetaData databaseMetaData = con.getMetaData();
this.dbType = databaseMetaData.getDatabaseProductName().toLowerCase();
this.dbVersion = databaseMetaData.getDatabaseProductVersion().toLowerCase();
if ((this.dbVersion != null) && this.dbVersion.toLowerCase().contains("mariadb")) {
this.mariaDb = true;
}
LOG.debug("Database product name: '{}'", dbType);
loadTableMetaData(databaseMetaData, con);
} catch (Exception ex) {
throw new ConnectionException("Failed to detect database product name and load metadata", ex);
}
this.creationResultCode = ResultCode.SUCCESS_INT_VALUE;
}
use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class SqlConnectionProvider method openWithWaitImpl.
private void openWithWaitImpl() throws Exception {
long connectionMaxWaitTimeMillis = StringHelper.toLong(props.getProperty("connection.pool.create-max-wait-time-millis"), 30 * 1000L);
LOG.debug("Using connection timeout: '{}'", connectionMaxWaitTimeMillis);
Exception lastException = null;
int attempt = 0;
long currentTime = System.currentTimeMillis();
long maxWaitTime = currentTime + connectionMaxWaitTimeMillis;
do {
attempt++;
if (attempt > 0) {
LOG.info("Attempting to create connection pool: '{}'", attempt);
}
try {
open();
if (isConnected()) {
break;
} else {
LOG.info("Failed to connect to DB");
destroy();
throw new ConnectionException("Failed to create connection pool");
}
} catch (Exception ex) {
lastException = ex;
}
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
LOG.error("Exception happened in sleep", ex);
return;
}
currentTime = System.currentTimeMillis();
} while (maxWaitTime > currentTime);
if (lastException != null) {
throw lastException;
}
}
Aggregations