use of java.sql.Connection in project tomcat by apache.
the class AbandonPercentageTest method testResetConnection.
@Test
public void testResetConnection() throws Exception {
int size = 1;
this.datasource.setMaxActive(size);
this.datasource.setMaxIdle(size);
this.datasource.setInitialSize(0);
this.datasource.getPoolProperties().setAbandonWhenPercentageFull(100);
this.datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(100);
this.datasource.getPoolProperties().setRemoveAbandoned(true);
this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
this.datasource.getPoolProperties().setJdbcInterceptors(ResetAbandonedTimer.class.getName());
Connection con = datasource.getConnection();
Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
for (int i = 0; i < 20; i++) {
Thread.sleep(200);
con.isClosed();
}
Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
con.close();
}
use of java.sql.Connection in project zeppelin by apache.
the class JDBCInterpreter method executeSql.
private InterpreterResult executeSql(String propertyKey, String sql, InterpreterContext interpreterContext) {
Connection connection;
Statement statement;
ResultSet resultSet = null;
String paragraphId = interpreterContext.getParagraphId();
String user = interpreterContext.getAuthenticationInfo().getUser();
InterpreterResult interpreterResult = new InterpreterResult(InterpreterResult.Code.SUCCESS);
try {
connection = getConnection(propertyKey, interpreterContext);
if (connection == null) {
return new InterpreterResult(Code.ERROR, "Prefix not found.");
}
ArrayList<String> multipleSqlArray = splitSqlQueries(sql);
for (int i = 0; i < multipleSqlArray.size(); i++) {
String sqlToExecute = multipleSqlArray.get(i);
statement = connection.createStatement();
if (statement == null) {
return new InterpreterResult(Code.ERROR, "Prefix not found.");
}
try {
getJDBCConfiguration(user).saveStatement(paragraphId, statement);
boolean isResultSetAvailable = statement.execute(sqlToExecute);
getJDBCConfiguration(user).setConnectionInDBDriverPoolSuccessful(propertyKey);
if (isResultSetAvailable) {
resultSet = statement.getResultSet();
// Regards that the command is DDL.
if (isDDLCommand(statement.getUpdateCount(), resultSet.getMetaData().getColumnCount())) {
interpreterResult.add(InterpreterResult.Type.TEXT, "Query executed successfully.");
} else {
interpreterResult.add(getResults(resultSet, !containsIgnoreCase(sqlToExecute, EXPLAIN_PREDICATE)));
}
} else {
// Response contains either an update count or there are no results.
int updateCount = statement.getUpdateCount();
interpreterResult.add(InterpreterResult.Type.TEXT, "Query executed successfully. Affected rows : " + updateCount);
}
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
/*ignored*/
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
/*ignored*/
}
}
}
}
//In case user ran an insert/update/upsert statement
if (connection != null) {
try {
if (!connection.getAutoCommit()) {
connection.commit();
}
connection.close();
} catch (SQLException e) {
/*ignored*/
}
}
getJDBCConfiguration(user).removeStatement(paragraphId);
} catch (Throwable e) {
if (e.getCause() instanceof TTransportException && Throwables.getStackTraceAsString(e).contains("GSS") && getJDBCConfiguration(user).isConnectionInDBDriverPoolSuccessful(propertyKey)) {
return reLoginFromKeytab(propertyKey, sql, interpreterContext, interpreterResult);
} else {
logger.error("Cannot run " + sql, e);
String errorMsg = Throwables.getStackTraceAsString(e);
try {
closeDBPool(user, propertyKey);
} catch (SQLException e1) {
logger.error("Cannot close DBPool for user, propertyKey: " + user + propertyKey, e1);
}
interpreterResult.add(errorMsg);
return new InterpreterResult(Code.ERROR, interpreterResult.message());
}
}
return interpreterResult;
}
use of java.sql.Connection in project cas by apereo.
the class QueryDatabaseAuthenticationHandlerTests method setUp.
@Before
public void setUp() throws Exception {
final Connection c = this.dataSource.getConnection();
final Statement s = c.createStatement();
c.setAutoCommit(true);
s.execute(getSqlInsertStatementToCreateUserAccount(0, Boolean.FALSE.toString(), Boolean.FALSE.toString()));
for (int i = 0; i < 10; i++) {
s.execute(getSqlInsertStatementToCreateUserAccount(i, Boolean.FALSE.toString(), Boolean.FALSE.toString()));
}
s.execute(getSqlInsertStatementToCreateUserAccount(20, Boolean.TRUE.toString(), Boolean.FALSE.toString()));
s.execute(getSqlInsertStatementToCreateUserAccount(21, Boolean.FALSE.toString(), Boolean.TRUE.toString()));
c.close();
}
use of java.sql.Connection in project cas by apereo.
the class QueryDatabaseAuthenticationHandlerTests method tearDown.
@After
public void tearDown() throws Exception {
final Connection c = this.dataSource.getConnection();
final Statement s = c.createStatement();
c.setAutoCommit(true);
for (int i = 0; i < 5; i++) {
s.execute("delete from casusers;");
}
c.close();
}
use of java.sql.Connection in project cas by apereo.
the class SearchModeSearchDatabaseAuthenticationHandlerTests method tearDown.
@After
public void tearDown() throws Exception {
final Connection c = this.dataSource.getConnection();
final Statement s = c.createStatement();
c.setAutoCommit(true);
for (int i = 0; i < 5; i++) {
s.execute("delete from casusers;");
}
c.close();
}
Aggregations