Search in sources :

Example 1 with SQLServerXADataSource

use of com.microsoft.sqlserver.jdbc.SQLServerXADataSource in project mssql-jdbc by Microsoft.

the class PoolingTest method testConnectionPoolReget.

@Test
public void testConnectionPoolReget() throws SQLException {
    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);
    PooledConnection pc = ds.getPooledConnection();
    Connection con = pc.getConnection();
    // now reget a connection
    Connection con2 = pc.getConnection();
    // assert that the first connection is closed.
    assertTrue(con.isClosed(), "First connection is not closed");
}
Also used : PooledConnection(javax.sql.PooledConnection) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) ISQLServerConnection(com.microsoft.sqlserver.jdbc.ISQLServerConnection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerXADataSource(com.microsoft.sqlserver.jdbc.SQLServerXADataSource) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 2 with SQLServerXADataSource

use of com.microsoft.sqlserver.jdbc.SQLServerXADataSource in project mssql-jdbc by Microsoft.

the class PoolingTest method testConnectionPoolConnFunctions.

@Test
public void testConnectionPoolConnFunctions() throws SQLException {
    String tableName = RandomUtil.getIdentifier("table");
    tableName = DBTable.escapeIdentifier(tableName);
    String sql1 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n" + "drop table " + tableName + "\n" + "create table " + tableName + "\n" + "(\n" + "wibble_id int primary key not null,\n" + "counter int null\n" + ");";
    String sql2 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n" + "drop table " + tableName + "\n";
    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);
    PooledConnection pc = ds.getPooledConnection();
    try (Connection con = pc.getConnection();
        Statement statement = con.createStatement()) {
        statement.execute(sql1);
        statement.execute(sql2);
        con.clearWarnings();
    }
    pc.close();
}
Also used : PooledConnection(javax.sql.PooledConnection) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) ISQLServerConnection(com.microsoft.sqlserver.jdbc.ISQLServerConnection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerXADataSource(com.microsoft.sqlserver.jdbc.SQLServerXADataSource) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 3 with SQLServerXADataSource

use of com.microsoft.sqlserver.jdbc.SQLServerXADataSource in project mssql-jdbc by Microsoft.

the class PoolingTest method testConnectionPoolClientConnectionId.

@Test
public void testConnectionPoolClientConnectionId() throws SQLException {
    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);
    PooledConnection pc = ds.getPooledConnection();
    ISQLServerConnection con = (ISQLServerConnection) pc.getConnection();
    UUID Id1 = con.getClientConnectionId();
    assertTrue(Id1 != null, "Unexecepted: ClientConnectionId is null from Pool");
    con.close();
    // now reget the connection
    ISQLServerConnection con2 = (ISQLServerConnection) pc.getConnection();
    UUID Id2 = con2.getClientConnectionId();
    con2.close();
    assertEquals(Id1, Id2, "ClientConnection Ids from pool are not the same.");
}
Also used : PooledConnection(javax.sql.PooledConnection) ISQLServerConnection(com.microsoft.sqlserver.jdbc.ISQLServerConnection) UUID(java.util.UUID) SQLServerXADataSource(com.microsoft.sqlserver.jdbc.SQLServerXADataSource) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 4 with SQLServerXADataSource

use of com.microsoft.sqlserver.jdbc.SQLServerXADataSource in project mssql-jdbc by Microsoft.

the class PoolingTest method testPooling.

@Test
public void testPooling() throws SQLException {
    assumeTrue(!DBConnection.isSqlAzure(DriverManager.getConnection(connectionString)), "Skipping test case on Azure SQL.");
    String randomTableName = RandomUtil.getIdentifier("table");
    // make the table a temporary table (will be created in tempdb database)
    String tempTableName = "#" + randomTableName;
    SQLServerXADataSource XADataSource1 = new SQLServerXADataSource();
    XADataSource1.setURL(connectionString);
    XADataSource1.setDatabaseName("tempdb");
    PooledConnection pc = XADataSource1.getPooledConnection();
    try (Connection conn = pc.getConnection()) {
        // create table in tempdb database
        conn.createStatement().execute("create table [" + tempTableName + "] (myid int)");
        conn.createStatement().execute("insert into [" + tempTableName + "] values (1)");
    }
    boolean tempTableFileRemoved = false;
    try (Connection conn = pc.getConnection()) {
        conn.createStatement().executeQuery("select * from [" + tempTableName + "]");
    } catch (SQLException e) {
        // make sure the temporary table is not found.
        if (e.getMessage().startsWith("Invalid object name")) {
            tempTableFileRemoved = true;
        }
    }
    assertTrue(tempTableFileRemoved, "Temporary table is not removed.");
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) ISQLServerConnection(com.microsoft.sqlserver.jdbc.ISQLServerConnection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerXADataSource(com.microsoft.sqlserver.jdbc.SQLServerXADataSource) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 5 with SQLServerXADataSource

use of com.microsoft.sqlserver.jdbc.SQLServerXADataSource in project mssql-jdbc by Microsoft.

the class PoolingTest method testConnectionPoolClose.

@Test
public void testConnectionPoolClose() throws SQLException {
    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);
    PooledConnection pc = ds.getPooledConnection();
    Connection con = pc.getConnection();
    pc.close();
    // assert that the first connection is closed.
    assertTrue(con.isClosed(), "Connection is not closed with pool close");
}
Also used : PooledConnection(javax.sql.PooledConnection) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) ISQLServerConnection(com.microsoft.sqlserver.jdbc.ISQLServerConnection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerXADataSource(com.microsoft.sqlserver.jdbc.SQLServerXADataSource) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Aggregations

SQLServerXADataSource (com.microsoft.sqlserver.jdbc.SQLServerXADataSource)7 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)7 Test (org.junit.jupiter.api.Test)7 ISQLServerConnection (com.microsoft.sqlserver.jdbc.ISQLServerConnection)5 PooledConnection (javax.sql.PooledConnection)5 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)4 Connection (java.sql.Connection)4 ISQLServerDataSource (com.microsoft.sqlserver.jdbc.ISQLServerDataSource)1 SQLServerConnectionPoolDataSource (com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource)1 SQLServerDataSource (com.microsoft.sqlserver.jdbc.SQLServerDataSource)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 UUID (java.util.UUID)1