Search in sources :

Example 91 with Connection

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();
}
Also used : Connection(java.sql.Connection) ResetAbandonedTimer(org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer) Test(org.junit.Test)

Example 92 with Connection

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;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) TTransportException(org.apache.thrift.transport.TTransportException)

Example 93 with Connection

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();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) Before(org.junit.Before)

Example 94 with Connection

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();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) After(org.junit.After)

Example 95 with Connection

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();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) After(org.junit.After)

Aggregations

Connection (java.sql.Connection)6326 PreparedStatement (java.sql.PreparedStatement)2793 ResultSet (java.sql.ResultSet)2657 Test (org.junit.Test)2455 SQLException (java.sql.SQLException)2267 Properties (java.util.Properties)1188 Statement (java.sql.Statement)1078 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)689 ArrayList (java.util.ArrayList)397 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)232 DataSource (javax.sql.DataSource)211 BaseTest (org.apache.phoenix.query.BaseTest)201 CallableStatement (java.sql.CallableStatement)192 IOException (java.io.IOException)158 Reader (java.io.Reader)144 DatabaseMetaData (java.sql.DatabaseMetaData)144 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)134 HashMap (java.util.HashMap)123 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)114 Timestamp (java.sql.Timestamp)113