use of java.sql.Connection in project tomcat by apache.
the class PoolableConnectionFactory method makeObject.
@Override
public PooledObject<PoolableConnection> makeObject() throws Exception {
Connection conn = _connFactory.createConnection();
if (conn == null) {
throw new IllegalStateException("Connection factory returned null from createConnection");
}
try {
initializeConnection(conn);
} catch (final SQLException sqle) {
// Make sure the connection is closed
try {
conn.close();
} catch (final SQLException ignore) {
// ignore
}
// Rethrow original exception so it is visible to caller
throw sqle;
}
final long connIndex = connectionIndex.getAndIncrement();
if (poolStatements) {
conn = new PoolingConnection(conn);
final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
config.setMaxTotalPerKey(-1);
config.setBlockWhenExhausted(false);
config.setMaxWaitMillis(0);
config.setMaxIdlePerKey(1);
config.setMaxTotal(maxOpenPreparedStatements);
if (dataSourceJmxName != null) {
final StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
base.append(Constants.JMX_CONNECTION_BASE_EXT);
base.append(Long.toString(connIndex));
config.setJmxNameBase(base.toString());
config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
} else {
config.setJmxEnabled(false);
}
final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>((PoolingConnection) conn, config);
((PoolingConnection) conn).setStatementPool(stmtPool);
((PoolingConnection) conn).setCacheState(_cacheState);
}
// Register this connection with JMX
ObjectName connJmxName;
if (dataSourceJmxName == null) {
connJmxName = null;
} else {
connJmxName = new ObjectName(dataSourceJmxName.toString() + Constants.JMX_CONNECTION_BASE_EXT + connIndex);
}
final PoolableConnection pc = new PoolableConnection(conn, _pool, connJmxName, _disconnectionSqlCodes, _fastFailValidation);
pc.setCacheState(_cacheState);
return new DefaultPooledObject<>(pc);
}
use of java.sql.Connection in project tomcat by apache.
the class JDBCStore method clear.
/**
* Remove all of the Sessions in this Store.
*
* @exception IOException if an input/output error occurs
*/
@Override
public void clear() throws IOException {
synchronized (this) {
int numberOfTries = 2;
while (numberOfTries > 0) {
Connection _conn = getConnection();
if (_conn == null) {
return;
}
try {
if (preparedClearSql == null) {
String clearSql = "DELETE FROM " + sessionTable + " WHERE " + sessionAppCol + " = ?";
preparedClearSql = _conn.prepareStatement(clearSql);
}
preparedClearSql.setString(1, getName());
preparedClearSql.execute();
// Break out after the finally block
numberOfTries = 0;
} catch (SQLException e) {
manager.getContext().getLogger().error(sm.getString(getStoreName() + ".SQLException", e));
if (dbConnection != null)
close(dbConnection);
} finally {
release(_conn);
}
numberOfTries--;
}
}
}
use of java.sql.Connection in project tomcat by apache.
the class JDBCStore method remove.
/**
* Remove the Session with the specified session identifier from
* this Store, if present. If no such Session is present, this method
* takes no action.
*
* @param id Session identifier of the Session to be removed
*
* @exception IOException if an input/output error occurs
*/
@Override
public void remove(String id) throws IOException {
synchronized (this) {
int numberOfTries = 2;
while (numberOfTries > 0) {
Connection _conn = getConnection();
if (_conn == null) {
return;
}
try {
remove(id, _conn);
// Break out after the finally block
numberOfTries = 0;
} catch (SQLException e) {
manager.getContext().getLogger().error(sm.getString(getStoreName() + ".SQLException", e));
if (dbConnection != null)
close(dbConnection);
} finally {
release(_conn);
}
numberOfTries--;
}
}
if (manager.getContext().getLogger().isDebugEnabled()) {
manager.getContext().getLogger().debug(sm.getString(getStoreName() + ".removing", id, sessionTable));
}
}
use of java.sql.Connection in project tomcat by apache.
the class JDBCStore method keys.
/**
* Return an array containing the session identifiers of all Sessions
* currently saved in this Store. If there are no such Sessions, a
* zero-length array is returned.
*
* @param expiredOnly flag, whether only keys of expired sessions should
* be returned
* @return array containing the list of session IDs
*
* @exception IOException if an input/output error occurred
*/
private String[] keys(boolean expiredOnly) throws IOException {
String[] keys = null;
synchronized (this) {
int numberOfTries = 2;
while (numberOfTries > 0) {
Connection _conn = getConnection();
if (_conn == null) {
return new String[0];
}
try {
String keysSql = "SELECT " + sessionIdCol + " FROM " + sessionTable + " WHERE " + sessionAppCol + " = ?";
if (expiredOnly) {
keysSql += " AND (" + sessionLastAccessedCol + " + " + sessionMaxInactiveCol + " * 1000 < ?)";
}
try (PreparedStatement preparedKeysSql = _conn.prepareStatement(keysSql)) {
preparedKeysSql.setString(1, getName());
if (expiredOnly) {
preparedKeysSql.setLong(2, System.currentTimeMillis());
}
try (ResultSet rst = preparedKeysSql.executeQuery()) {
ArrayList<String> tmpkeys = new ArrayList<>();
if (rst != null) {
while (rst.next()) {
tmpkeys.add(rst.getString(1));
}
}
keys = tmpkeys.toArray(new String[tmpkeys.size()]);
// Break out after the finally block
numberOfTries = 0;
}
}
} catch (SQLException e) {
manager.getContext().getLogger().error(sm.getString(getStoreName() + ".SQLException", e));
keys = new String[0];
// Close the connection so that it gets reopened next time
if (dbConnection != null)
close(dbConnection);
} finally {
release(_conn);
}
numberOfTries--;
}
}
return keys;
}
use of java.sql.Connection in project tomcat by apache.
the class JDBCStore method load.
/**
* Load the Session associated with the id <code>id</code>.
* If no such session is found <code>null</code> is returned.
*
* @param id a value of type <code>String</code>
* @return the stored <code>Session</code>
* @exception ClassNotFoundException if an error occurs
* @exception IOException if an input/output error occurred
*/
@Override
public Session load(String id) throws ClassNotFoundException, IOException {
StandardSession _session = null;
org.apache.catalina.Context context = getManager().getContext();
Log contextLog = context.getLogger();
synchronized (this) {
int numberOfTries = 2;
while (numberOfTries > 0) {
Connection _conn = getConnection();
if (_conn == null) {
return null;
}
ClassLoader oldThreadContextCL = context.bind(Globals.IS_SECURITY_ENABLED, null);
try {
if (preparedLoadSql == null) {
String loadSql = "SELECT " + sessionIdCol + ", " + sessionDataCol + " FROM " + sessionTable + " WHERE " + sessionIdCol + " = ? AND " + sessionAppCol + " = ?";
preparedLoadSql = _conn.prepareStatement(loadSql);
}
preparedLoadSql.setString(1, id);
preparedLoadSql.setString(2, getName());
try (ResultSet rst = preparedLoadSql.executeQuery()) {
if (rst.next()) {
try (ObjectInputStream ois = getObjectInputStream(rst.getBinaryStream(2))) {
if (contextLog.isDebugEnabled()) {
contextLog.debug(sm.getString(getStoreName() + ".loading", id, sessionTable));
}
_session = (StandardSession) manager.createEmptySession();
_session.readObjectData(ois);
_session.setManager(manager);
}
} else if (context.getLogger().isDebugEnabled()) {
contextLog.debug(getStoreName() + ": No persisted data object found");
}
// Break out after the finally block
numberOfTries = 0;
}
} catch (SQLException e) {
contextLog.error(sm.getString(getStoreName() + ".SQLException", e));
if (dbConnection != null)
close(dbConnection);
} finally {
context.unbind(Globals.IS_SECURITY_ENABLED, oldThreadContextCL);
release(_conn);
}
numberOfTries--;
}
}
return _session;
}
Aggregations