Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ public class AESetup extends AbstractTest {
*/
@BeforeAll
public static void setUpConnection() throws TestAbortedException, Exception {
assumeTrue(13 <= new DBConnection(connectionString).getServerVersion(),
TestResource.getResource("R_Incompat_SQLServerVersion"));
try (DBConnection con = new DBConnection(connectionString)) {
assumeTrue(13 <= con.getServerVersion(), TestResource.getResource("R_Incompat_SQLServerVersion"));
}

String AETestConenctionString = connectionString + ";sendTimeAsDateTime=false";
readFromFile(javaKeyStoreInputFile, "Alias name");
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@ public class BulkCopyISQLServerBulkRecordTest extends AbstractTest {

@Test
public void testISQLServerBulkRecord() throws SQLException {
DBTable dstTable = null;
try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) {
DBTable dstTable = new DBTable(true);
dstTable = new DBTable(true);
stmt.createTable(dstTable);
BulkData Bdata = new BulkData(dstTable);

BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, Bdata, dstTable);
} finally {
if (null != dstTable) {
try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) {
stmt.dropTable(dstTable);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterAll;

import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
Expand Down Expand Up @@ -214,4 +215,16 @@ private static void createTables(Statement stmt) throws SQLException {
+ " (c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null, c4 char(7000));";
stmt.execute(sql);
}

/**
* drops tables
*
* @throws SQLException
*/
@AfterAll
public static void terminate() throws SQLException {
try (Connection conn = DriverManager.getConnection(connectionString); Statement stmt = conn.createStatement()) {
dropTables(stmt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.sql.Connection;
Expand Down Expand Up @@ -53,7 +54,7 @@ public class ConnectionDriverTest extends AbstractTest {
String randomServer = RandomUtil.getIdentifier("Server");

/**
* test SSL properties
* test connection properties
*
* @throws SQLException
*/
Expand Down Expand Up @@ -92,7 +93,7 @@ public void testConnectionDriver() throws SQLException {
}

/**
* test SSL properties with SQLServerDataSource
* test connection properties with SQLServerDataSource
*/
@Test
public void testDataSource() {
Expand Down Expand Up @@ -229,15 +230,17 @@ public void testConnectionClosed() throws SQLException {
SQLServerDataSource mds = new SQLServerDataSource();
mds.setURL(connectionString);
Connection con = mds.getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

boolean exceptionThrown = false;
try {
stmt.executeUpdate("RAISERROR ('foo', 20,1) WITH LOG");
try (Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
boolean exceptionThrown = false;
try {
stmt.executeUpdate("RAISERROR ('foo', 20,1) WITH LOG");
} catch (Exception e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown, TestResource.getResource("R_expectedExceptionNotThrown"));
} catch (Exception e) {
exceptionThrown = true;
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.toString());
}
assertTrue(exceptionThrown, TestResource.getResource("R_expectedExceptionNotThrown"));

// check to make sure that connection is closed.
assertTrue(con.isClosed(), TestResource.getResource("R_connectionIsNotClosed"));
Expand Down Expand Up @@ -302,15 +305,15 @@ public void testDeadConnection() throws SQLException {
assumeTrue(!DBConnection.isSqlAzure(DriverManager.getConnection(connectionString)),
TestResource.getResource("R_skipAzure"));

String tableName = null;
try (SQLServerConnection conn = (SQLServerConnection) DriverManager
.getConnection(connectionString + ";responseBuffering=adaptive")) {
.getConnection(connectionString + ";responseBuffering=adaptive");
Statement stmt = conn.createStatement()) {

Statement stmt = null;
String tableName = RandomUtil.getIdentifier("Table");
tableName = RandomUtil.getIdentifier("Table");
tableName = DBTable.escapeIdentifier(tableName);

conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.executeUpdate("CREATE TABLE " + tableName + " (col1 int primary key)");
for (int i = 0; i < 80; i++) {
stmt.executeUpdate("INSERT INTO " + tableName + "(col1) values (" + i + ")");
Expand All @@ -322,10 +325,16 @@ public void testDeadConnection() throws SQLException {
} catch (SQLException e) {
assertEquals(e.getMessage(), TestResource.getResource("R_connectionReset"),
TestResource.getResource("R_unknownException"));
} finally {
DriverManager.getConnection(connectionString).createStatement().execute("drop table " + tableName);
}
assertEquals(conn.isValid(5), false, TestResource.getResource("R_deadConnection"));
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.toString());
} finally {
try (SQLServerConnection conn = (SQLServerConnection) DriverManager
.getConnection(connectionString + ";responseBuffering=adaptive");
Statement stmt = conn.createStatement()) {
stmt.execute("drop table " + tableName);
}
}
}

Expand Down
105 changes: 55 additions & 50 deletions src/test/java/com/microsoft/sqlserver/jdbc/connection/PoolingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.lang.management.ManagementFactory;
Expand Down Expand Up @@ -58,16 +59,16 @@ public void testPooling() throws SQLException {
XADataSource1.setDatabaseName("tempdb");

PooledConnection pc = XADataSource1.getPooledConnection();
try (Connection conn = pc.getConnection()) {
try (Connection conn = pc.getConnection(); Statement stmt = conn.createStatement()) {

// create table in tempdb database
conn.createStatement().execute("create table [" + tempTableName + "] (myid int)");
conn.createStatement().execute("insert into [" + tempTableName + "] values (1)");
stmt.execute("create table [" + tempTableName + "] (myid int)");
stmt.execute("insert into [" + tempTableName + "] values (1)");
}

boolean tempTableFileRemoved = false;
try (Connection conn = pc.getConnection()) {
conn.createStatement().executeQuery("select * from [" + tempTableName + "]");
try (Connection conn = pc.getConnection(); Statement stmt = conn.createStatement()) {
stmt.executeQuery("select * from [" + tempTableName + "]");
} catch (SQLException e) {
// make sure the temporary table is not found.
if (e.getMessage().startsWith(TestResource.getResource("R_invalidObjectName"))) {
Expand All @@ -81,15 +82,19 @@ public void testPooling() throws SQLException {
public void testConnectionPoolReget() throws SQLException {
SQLServerXADataSource ds = new SQLServerXADataSource();
ds.setURL(connectionString);
PooledConnection pc = null;
try {
pc = ds.getPooledConnection();
try (Connection con = pc.getConnection(); Connection con2 = pc.getConnection()) {

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(), TestResource.getResource("R_firstConnectionNotClosed"));
// assert that the first connection is closed.
assertTrue(con.isClosed(), TestResource.getResource("R_firstConnectionNotClosed"));
}
} finally {
if (null != pc) {
pc.close();
}
}
Comment thread
lilgreenbird marked this conversation as resolved.
}

@Test
Expand All @@ -111,8 +116,14 @@ public void testConnectionPoolConnFunctions() throws SQLException {
statement.execute(sql1);
statement.execute(sql2);
con.clearWarnings();

} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.toString());
} finally {
if (null != pc) {
pc.close();
}
}
pc.close();
}

@Test
Expand All @@ -121,32 +132,45 @@ public void testConnectionPoolClose() throws SQLException {
ds.setURL(connectionString);

PooledConnection pc = ds.getPooledConnection();
Connection con = pc.getConnection();
try (Connection con = pc.getConnection()) {
pc.close();

pc.close();
// assert that the first connection is closed.
assertTrue(con.isClosed(), TestResource.getResource("R_connectionNotClosedWithPoolClose"));
// assert that the first connection is closed.
assertTrue(con.isClosed(), TestResource.getResource("R_connectionNotClosedWithPoolClose"));
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.toString());
} finally {
if (null != pc) {
pc.close();
}
}
}

@Test
public void testConnectionPoolClientConnectionId() throws SQLException {
SQLServerXADataSource ds = new SQLServerXADataSource();
ds.setURL(connectionString);
PooledConnection pc = null;
try {
pc = ds.getPooledConnection();
ISQLServerConnection con = (ISQLServerConnection) pc.getConnection();

PooledConnection pc = ds.getPooledConnection();
ISQLServerConnection con = (ISQLServerConnection) pc.getConnection();

UUID Id1 = con.getClientConnectionId();
assertTrue(Id1 != null, TestResource.getResource("R_connectionNotClosedWithPoolClose"));
con.close();
UUID Id1 = con.getClientConnectionId();
assertTrue(Id1 != null, TestResource.getResource("R_connectionNotClosedWithPoolClose"));
con.close();

// now reget the connection
ISQLServerConnection con2 = (ISQLServerConnection) pc.getConnection();
// now reget the connection
ISQLServerConnection con2 = (ISQLServerConnection) pc.getConnection();

UUID Id2 = con2.getClientConnectionId();
con2.close();
UUID Id2 = con2.getClientConnectionId();
con2.close();

assertEquals(Id1, Id2, TestResource.getResource("R_idFromPoolNotSame"));
assertEquals(Id1, Id2, TestResource.getResource("R_idFromPoolNotSame"));
} finally {
if (null != pc) {
pc.close();
}
}
}

/**
Expand Down Expand Up @@ -191,34 +215,15 @@ public void testApacheDBCP() throws SQLException {
* @throws SQLException
*/
private static void connect(DataSource ds) throws SQLException {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;

try {
con = ds.getConnection();
pst = con.prepareStatement("SELECT SUSER_SNAME()");
pst.setQueryTimeout(5);
rs = pst.executeQuery();
try (Connection con = ds.getConnection(); PreparedStatement pst = con.prepareStatement("SELECT SUSER_SNAME()");
ResultSet rs = pst.executeQuery()) {

// TODO : we are commenting this out due to AppVeyor failures. Will investigate later.
// assertTrue(countTimeoutThreads() >= 1, "Timeout timer is missing.");

while (rs.next()) {
rs.getString(1);
}
} finally {
if (rs != null) {
rs.close();
}

if (pst != null) {
pst.close();
}

if (con != null) {
con.close();
}
}
}

Expand Down
Loading