Example usage for javax.sql DataSource getConnection

List of usage examples for javax.sql DataSource getConnection

Introduction

In this page you can find the example usage for javax.sql DataSource getConnection.

Prototype

Connection getConnection() throws SQLException;

Source Link

Document

Attempts to establish a connection with the data source that this DataSource object represents.

Usage

From source file:org.apereo.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDaoTest.java

@Override
protected void tearDownSchema(final DataSource dataSource) throws SQLException {
    final Connection con = dataSource.getConnection();

    con.prepareStatement("DROP TABLE user_table").execute();
    con.prepareStatement("SHUTDOWN").execute();

    con.close();//from   w  w w. j  a  v  a2  s  .c o  m
}

From source file:com.p6spy.engine.spy.XADataSourceTest.java

private void insertData(DataSource ds) {
    Connection connection = null;
    try {// ww  w . j  a v  a  2 s.  co m
        connection = ds.getConnection();
        P6TestUtil.execute(connection, "insert into customers(id,name) values (50,'foo')");
    } catch (SQLException e) {
        e.printStackTrace();
        Assert.fail();
    } finally {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.flywaydb.core.internal.dbsupport.mysql.MySQLMigrationTestCase.java

/**
 * Tests whether locking problems occur when Flyway's DB connection gets reused.
 *//*w w w  . ja v a 2  s .  co m*/
@Test
public void lockOnConnectionReUse() throws SQLException {
    DataSource twoConnectionsDataSource = new TwoConnectionsDataSource(flyway.getDataSource());
    flyway.setDataSource(twoConnectionsDataSource);
    flyway.setLocations(getBasedir());
    flyway.migrate();

    Connection connection1 = twoConnectionsDataSource.getConnection();
    Connection connection2 = twoConnectionsDataSource.getConnection();
    assertEquals(2, new JdbcTemplate(connection1, 0).queryForInt("SELECT COUNT(*) FROM test_user"));
    assertEquals(2, new JdbcTemplate(connection2, 0).queryForInt("SELECT COUNT(*) FROM test_user"));
}

From source file:org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDaoTest.java

@Override
protected void setUpSchema(DataSource dataSource) throws SQLException {
    Connection con = dataSource.getConnection();

    con.prepareStatement("CREATE TABLE user_table " + "(netid VARCHAR, " + "name VARCHAR, " + "email VARCHAR, "
            + "shirt_color VARCHAR)").execute();

    con.prepareStatement("INSERT INTO user_table " + "(netid, name, email, shirt_color) "
            + "VALUES ('awp9', 'Andrew', 'andrew.petro@yale.edu', 'blue')").execute();

    con.prepareStatement("INSERT INTO user_table " + "(netid, name, email, shirt_color) "
            + "VALUES ('edalquist', 'Eric', 'edalquist@unicon.net', 'blue')").execute();

    con.prepareStatement("INSERT INTO user_table " + "(netid, name, email, shirt_color) "
            + "VALUES ('atest', 'Andrew', 'andrew.test@test.net', 'red')").execute();

    con.prepareStatement("INSERT INTO user_table " + "(netid, name, email, shirt_color) "
            + "VALUES ('susan', 'Susan', 'susan.test@test.net', null)").execute();

    con.close();/*w w w  .j a v  a  2 s  .c  o  m*/
}

From source file:com.p6spy.engine.spy.XADataSourceTest.java

private int queryForInt(DataSource ds) {
    Connection connection = null;
    try {//from  w ww  .j a va  2s .  c  o  m
        connection = ds.getConnection();

        // add different data to each connection
        return P6TestUtil.queryForInt(connection, "select count(*) from customers where id=50");
    } catch (SQLException e) {
        e.printStackTrace();
        Assert.fail();
    } finally {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return -1;
}

From source file:com.fingerprintsoft.vitunit.runner.model.VitUnitTestClass.java

protected void updateSequences(DataSource dataSource, String[] sql) {
    Connection connection = null;
    Statement statement = null;//from  ww  w  .  j a v a  2s  .co m

    try {
        connection = dataSource.getConnection();

        if (sql != null) {
            for (String query : sql) {
                if (StringUtils.isNotBlank(query)) {
                    statement = connection.createStatement();
                    statement.executeUpdate(query);
                }
            }
        }
    } catch (SQLException e) {
        logger.warn("An error occured while updating the sequences", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // Do nothing
            }
        }
    }

}

From source file:azkaban.trigger.JdbcTriggerLoaderTest.java

public void setupDB() {
    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);//from   ww  w.j av  a2 s . c  o m
    testDBExists = true;

    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    CountHandler countHandler = new CountHandler();
    QueryRunner runner = new QueryRunner();
    try {
        runner.query(connection, "SELECT COUNT(1) FROM triggers", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);

    clearDB();
}

From source file:com.saasovation.common.port.adapter.persistence.eventsourcing.mysql.MySQLJDBCEventStoreTest.java

private long greatestEventId() {
    long greatestEventId = 0;

    DataSource dataSource = (DataSource) applicationContext.getBean("eventStoreDataSource");
    Connection connection = null;
    ResultSet result = null;// w  w  w .ja  v a  2  s.c  om

    try {
        connection = dataSource.getConnection();

        PreparedStatement statement = dataSource.getConnection()
                .prepareStatement("SELECT MAX(event_id) from tbl_es_event_store");

        result = statement.executeQuery();

        if (result.next()) {
            greatestEventId = result.getLong(1);
        }

    } catch (Throwable t) {

    } finally {
        if (result != null) {
            try {
                result.close();
            } catch (Throwable t) {
                // ignore
            }
        }
        try {
            connection.close();
        } catch (Throwable t) {
            // ignore
        }
    }

    return greatestEventId;
}

From source file:org.apache.openjpa.jdbc.sql.AzureDictionary.java

public String[] getCreateTableSQL(final Table table, final Federation federation) {
    final List<String> toBeCreated = new ArrayList<String>();

    final DataSource dataSource = conf.getDataSource2(null);
    if (dataSource != null) {
        Connection conn = null;/*from ww  w. ja v  a 2s. c  o m*/
        try {
            conn = dataSource.getConnection();

            if (federation == null) {
                toBeCreated.add(getCreateTableStm(table));
            } else {
                // perform use federation and create table
                toBeCreated.addAll(getStatements(table, federation));
            }
        } catch (SQLException e) {
            log.error("Error creating schema", e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error("Error closing connection", e);
                }
            }
        }
    }

    return toBeCreated.toArray(new String[toBeCreated.size()]);
}