@Override public Connection getConnection() throws PersistenceException { Connection connection; try { connection = dataSource.getConnection(); } catch (SQLException e) { log.sqlFailureRetrievingConnection(e); throw new PersistenceException("This might be related to https://jira.jboss.org/browse/ISPN-604", e); } if (trace) { log.tracef("Connection checked out: %s", connection); } return connection; }
private void log(Connection connection, boolean checkout, boolean before) { if (trace) { String stage = before ? "before" : "after"; String operation = checkout ? "checkout" : "release"; try { log.tracef("DataSource %s %s (NumBusyConnectionsAllUsers) : %d, (NumConnectionsAllUsers) : %d", stage, operation, getNumBusyConnectionsAllUsers(), getNumConnectionsAllUsers()); } catch (SQLException e) { log.sqlFailureUnexpected(e); } if (connection != null) log.tracef("Connection %s : %s", operation, connection); } } }
private void loadDriver(String driverClass, ClassLoader classLoader) throws PersistenceException { if (log.isTraceEnabled()) log.tracef("Attempting to load driver %s", driverClass); Util.getInstance(driverClass, classLoader); }
private void executeUpsert(Connection connection, MarshalledEntry entry, String keyStr, int segment) throws InterruptedException, SQLException { PreparedStatement ps = null; String sql = tableManager.getUpsertRowSql(); if (trace) { log.tracef("Running sql '%s'. Key string is '%s'", sql, keyStr); } try { ps = connection.prepareStatement(sql); prepareUpsertStatement(entry, keyStr, segment, ps); ps.executeUpdate(); } finally { JdbcUtil.safeClose(ps); } }
public boolean tableExists(Connection connection, TableName tableName) throws PersistenceException { Objects.requireNonNull(tableName, "table name is mandatory"); ResultSet rs = null; try { // we need to make sure, that (even if the user has extended permissions) only the tables in current schema are checked // explicit set of the schema to the current user one to make sure only tables of the current users are requested DatabaseMetaData metaData = connection.getMetaData(); String schemaPattern = tableName.getSchema(); rs = metaData.getTables(null, schemaPattern, tableName.getName(), new String[]{"TABLE"}); return rs.next(); } catch (SQLException e) { if (log.isTraceEnabled()) log.tracef(e, "SQLException occurs while checking the table %s", tableName); return false; } finally { JdbcUtil.safeClose(rs); } }
@Override public boolean tableExists(Connection connection, TableName tableName) throws PersistenceException { Objects.requireNonNull(tableName, "table name is mandatory"); ResultSet rs = null; try { DatabaseMetaData metaData = connection.getMetaData(); String schemaPattern = tableName.getSchema() == null ? metaData.getUserName() : tableName.getSchema(); rs = metaData.getTables(null, schemaPattern, tableName.getName(), new String[]{"TABLE"}); return rs.next(); } catch (SQLException e) { if (LOG.isTraceEnabled()) LOG.tracef(e, "SQLException occurs while checking the table %s", tableName); return false; } finally { JdbcUtil.safeClose(rs); } }
private void executeLegacyUpdate(Connection connection, MarshalledEntry entry, String keyStr, int segment) throws InterruptedException, SQLException { String sql = tableManager.getSelectIdRowSql(); if (trace) { log.tracef("Running sql '%s'. Key string is '%s'", sql, keyStr); } PreparedStatement ps = null; try { ps = connection.prepareStatement(sql); ps.setString(1, keyStr); ResultSet rs = ps.executeQuery(); boolean update = rs.next(); if (update) { sql = tableManager.getUpdateRowSql(); } else { sql = tableManager.getInsertRowSql(); } JdbcUtil.safeClose(rs); JdbcUtil.safeClose(ps); if (trace) { log.tracef("Running sql '%s'. Key string is '%s'", sql, keyStr); } ps = connection.prepareStatement(sql); prepareStatement(entry, keyStr, segment, ps, !update); ps.executeUpdate(); } finally { JdbcUtil.safeClose(ps); } }
@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"); } connectionPool = C3P0ConnectionPool.forceC3P0() ? new C3P0ConnectionPool(classLoader, poolConfig) : new HikariConnectionPool(classLoader, poolConfig); if (trace) log.tracef("Started connection factory with config: %s", config); }
@Override public boolean delete(Object key) { Connection connection = null; PreparedStatement ps = null; String keyStr = key2Str(key); try { String sql = tableManager.getDeleteRowSql(); if (trace) { log.tracef("Running sql '%s' on %s", sql, keyStr); } connection = connectionFactory.getConnection(); ps = connection.prepareStatement(sql); ps.setString(1, keyStr); return ps.executeUpdate() == 1; } catch (SQLException ex) { log.sqlFailureRemovingKeys(ex); throw new PersistenceException("Error while removing string keys from database", ex); } finally { JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(connection); } }
private void createIndex(Connection conn, String indexExt, String columnName) throws PersistenceException { if (metaData.isIndexingDisabled()) return; boolean indexExists = indexExists(getIndexName(false, indexExt), conn); if (!indexExists) { String ddl = String.format("CREATE INDEX %s ON %s (%s)", getIndexName(true, indexExt), getTableName(), columnName); if (log.isTraceEnabled()) { log.tracef("Adding index with following DDL: '%s'.", ddl); } executeUpdateSql(conn, ddl); } }
protected void dropIndex(Connection conn, String indexName) throws PersistenceException { if (!indexExists(getIndexName(true, indexName), conn)) return; String dropIndexDdl = getDropTimestampSql(indexName); if (log.isTraceEnabled()) { log.tracef("Dropping timestamp index with '%s'", dropIndexDdl); } executeUpdateSql(conn, dropIndexDdl); }
@Override public void clear() { Connection conn = null; Statement statement = null; try { String sql = tableManager.getDeleteAllRowsSql(); conn = connectionFactory.getConnection(); statement = conn.createStatement(); int result = statement.executeUpdate(sql); if (log.isTraceEnabled()) { log.tracef("Successfully removed %d rows.", result); } } catch (SQLException ex) { log.failedClearingJdbcCacheStore(ex); throw new PersistenceException("Failed clearing cache store", ex); } finally { JdbcUtil.safeClose(statement); connectionFactory.releaseConnection(conn); } }
@Override public void stop() { Throwable cause = null; try { tableManager.stop(); tableManager = null; } catch (Throwable t) { cause = t.getCause(); if (cause == null) cause = t; log.debug("Exception while stopping", t); } try { if (configuration.connectionFactory() instanceof ManagedConnectionFactory) { log.tracef("Stopping managed connection factory: %s", connectionFactory); connectionFactory.stop(); } } catch (Throwable t) { if (cause == null) { cause = t; } else { t.addSuppressed(cause); } log.debug("Exception while stopping", t); } if (cause != null) { throw new PersistenceException("Exceptions occurred while stopping store", cause); } }
@Override public void start() { if (configuration.manageConnectionFactory()) { ConnectionFactory factory = ConnectionFactory.getConnectionFactory(configuration.connectionFactory().connectionFactoryClass()); factory.start(configuration.connectionFactory(), factory.getClass().getClassLoader()); initializeConnectionFactory(factory); } try { Object mapper = Util.loadClassStrict(configuration.key2StringMapper(), globalConfiguration.classLoader()).newInstance(); if (mapper instanceof Key2StringMapper) key2StringMapper = (Key2StringMapper) mapper; } catch (Exception e) { log.errorf("Trying to instantiate %s, however it failed due to %s", configuration.key2StringMapper(), e.getClass().getName()); throw new IllegalStateException("This should not happen.", e); } if (trace) { log.tracef("Using key2StringMapper: %s", key2StringMapper.getClass().getName()); } if (configuration.preload()) { enforceTwoWayMapper("preload"); } if (isDistributedCache) { enforceTwoWayMapper("distribution/rehashing"); } }
dataSource = (DataSource) ctx.lookup(datasourceName); if (trace) { log.tracef("Datasource lookup for %s succeeded: %b", datasourceName, dataSource);
public void dropTable(Connection conn) throws PersistenceException { dropIndex(conn, timestampIndexExt); dropIndex(conn, segmentIndexExt); String clearTable = "DELETE FROM " + getTableName(); executeUpdateSql(conn, clearTable); String dropTableDdl = "DROP TABLE " + getTableName(); if (log.isTraceEnabled()) { log.tracef("Dropping table with following DDL '%s'", dropTableDdl); } executeUpdateSql(conn, dropTableDdl); }
@Override public void clear(IntSet segments) { Connection conn = null; PreparedStatement ps = null; try { String sql = tableManager.getDeleteRowsSqlForSegments(segments.size()); conn = connectionFactory.getConnection(); ps = conn.prepareStatement(sql); int offset = 0; for (PrimitiveIterator.OfInt segIter = segments.iterator(); segIter.hasNext(); ) { ps.setInt(++offset, segIter.nextInt()); } int result = ps.executeUpdate(); if (log.isTraceEnabled()) { log.tracef("Successfully removed %d rows.", result); } } catch (SQLException ex) { log.failedClearingJdbcCacheStore(ex); throw new PersistenceException("Failed clearing cache store when using segments " + segments, ex); } finally { JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(conn); } }
log.tracef("Running sql %s", sql);
@Override public void start(ConnectionFactoryConfiguration config, ClassLoader classLoader) throws PersistenceException { SimpleConnectionFactoryConfiguration factoryConfiguration; if (config instanceof SimpleConnectionFactoryConfiguration) { factoryConfiguration = (SimpleConnectionFactoryConfiguration) config; } else { throw new PersistenceException("ConnectionFactoryConfiguration has to be an instance of " + "SimpleConnectionFactoryConfiguration."); } loadDriver(factoryConfiguration.driverClass(), classLoader); this.connectionUrl = factoryConfiguration.connectionUrl(); this.userName = factoryConfiguration.username(); this.password = factoryConfiguration.password(); if (trace) { log.tracef("Starting connection %s", this); } }
public void createTable(Connection conn) throws PersistenceException { if (cacheName == null || cacheName.trim().length() == 0) throw new PersistenceException("cacheName needed in order to create table"); String ddl; if (metaData.isSegmentedDisabled()) { ddl = String.format("CREATE TABLE %1$s (%2$s %3$s NOT NULL, %4$s %5$s NOT NULL, %6$s %7$s NOT NULL, PRIMARY KEY (%2$s))", getTableName(), config.idColumnName(), config.idColumnType(), config.dataColumnName(), config.dataColumnType(), config.timestampColumnName(), config.timestampColumnType()); } else { ddl = String.format("CREATE TABLE %1$s (%2$s %3$s NOT NULL, %4$s %5$s NOT NULL, %6$s %7$s NOT NULL, %8$s %9$s NOT NULL, PRIMARY KEY (%2$s))", getTableName(), config.idColumnName(), config.idColumnType(), config.dataColumnName(), config.dataColumnType(), config.timestampColumnName(), config.timestampColumnType(), config.segmentColumnName(), config.segmentColumnType()); } if (log.isTraceEnabled()) { log.tracef("Creating table with following DDL: '%s'.", ddl); } executeUpdateSql(conn, ddl); }