Search in sources :

Example 21 with SQLServerDataSource

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

the class ConnectionDriverTest method testInvalidCombination.

@Test
public void testInvalidCombination() throws SQLException {
    long timerStart = 0;
    long timerEnd = 0;
    Connection con = null;
    final long milsecs = threshHoldForNoRetryInMilliseconds;
    try {
        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setURL(connectionString);
        ds.setLoginTimeout(loginTimeOutInSeconds);
        ds.setMultiSubnetFailover(true);
        ds.setFailoverPartner(RandomUtil.getIdentifier("FailoverPartner"));
        timerStart = System.currentTimeMillis();
        con = ds.getConnection();
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Connecting to a mirrored"));
        timerEnd = System.currentTimeMillis();
    }
    long timeDiff = timerEnd - timerStart;
    assertTrue(con == null, "Should not have connected.");
    assertTrue(timeDiff <= milsecs, "Exited in more than " + (milsecs / 1000) + " seconds.");
}
Also used : SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) ISQLServerConnection(com.microsoft.sqlserver.jdbc.ISQLServerConnection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 22 with SQLServerDataSource

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

the class StatementCancellationTest method setMultiSubnetFailoverToTrue.

/**
 * Tests Statement Cancellation works when MultiSubnetFailover is set to true
 */
@Test
public void setMultiSubnetFailoverToTrue() throws SQLException {
    long timeStart = 0;
    long timeEnd = 0;
    try {
        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setURL(connectionString);
        ds.setMultiSubnetFailover(true);
        try (Connection conn = ds.getConnection()) {
            try (final Statement stmt = conn.createStatement()) {
                final Thread cancellationThread = new Thread() {

                    public void run() {
                        try {
                            Thread.sleep(CANCEL_WAIT_MILLISECONDS);
                            stmt.cancel();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                cancellationThread.setName("stmtCancel");
                cancellationThread.start();
                try {
                    timeStart = System.currentTimeMillis();
                    stmt.execute("WAITFOR DELAY '00:00:" + (DELAY_WAIT_MILLISECONDS / 1000) + "'");
                } catch (SQLException e) {
                    assertTrue(e.getMessage().startsWith("The query was canceled"), "Unexpected error message.");
                }
            }
        }
    } finally {
        timeEnd = System.currentTimeMillis();
        long timeDifference = timeEnd - timeStart;
        assertTrue(timeDifference >= CANCEL_WAIT_MILLISECONDS, "Cancellation failed.");
        assertTrue(timeDifference < DELAY_WAIT_MILLISECONDS, "Cancellation failed.");
        assertTrue((timeDifference - CANCEL_WAIT_MILLISECONDS) < 1000, "Cancellation failed.");
    }
}
Also used : SQLException(java.sql.SQLException) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Statement(java.sql.Statement) Connection(java.sql.Connection) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 23 with SQLServerDataSource

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

the class CallableStatementTest method getSetNullWithTypeVarchar.

/**
 * test for setNull(index, varchar) to behave as setNull(index, nvarchar) when SendStringParametersAsUnicode is true
 *
 * @throws SQLException
 */
@Test
public void getSetNullWithTypeVarchar() throws SQLException {
    String polishchar = "\u0143";
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setURL(connectionString);
    ds.setSendStringParametersAsUnicode(true);
    String sql = "{? = call " + setNullProcedureName + " (?,?)}";
    try (Connection connection = ds.getConnection();
        SQLServerCallableStatement cs = (SQLServerCallableStatement) connection.prepareCall(sql);
        SQLServerCallableStatement cs2 = (SQLServerCallableStatement) connection.prepareCall(sql)) {
        cs.registerOutParameter(1, Types.INTEGER);
        cs.setString(2, polishchar);
        cs.setString(3, null);
        cs.registerOutParameter(3, Types.VARCHAR);
        cs.execute();
        String expected = cs.getString(3);
        cs2.registerOutParameter(1, Types.INTEGER);
        cs2.setString(2, polishchar);
        cs2.setNull(3, Types.VARCHAR);
        cs2.registerOutParameter(3, Types.NVARCHAR);
        cs2.execute();
        String actual = cs2.getString(3);
        assertEquals(expected, actual);
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 24 with SQLServerDataSource

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

the class NativeMSSQLDataSourceTest method testDSNormal.

@Test
public void testDSNormal() throws ClassNotFoundException, IOException, SQLException {
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setURL(connectionString);
    try (Connection conn = ds.getConnection()) {
    }
    ds = testSerial(ds);
    try (Connection conn = ds.getConnection()) {
    }
}
Also used : ISQLServerDataSource(com.microsoft.sqlserver.jdbc.ISQLServerDataSource) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 25 with SQLServerDataSource

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

the class NativeMSSQLDataSourceTest method testInterfaceWrapping.

@Test
public void testInterfaceWrapping() throws ClassNotFoundException, SQLException {
    SQLServerDataSource ds = new SQLServerDataSource();
    assertEquals(true, ds.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource")));
    assertEquals(true, ds.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDataSource")));
    assertEquals(true, ds.isWrapperFor(Class.forName("javax.sql.CommonDataSource")));
    ISQLServerDataSource ids = (ISQLServerDataSource) (ds.unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource")));
    ids.setApplicationName("AppName");
    SQLServerConnectionPoolDataSource poolDS = new SQLServerConnectionPoolDataSource();
    assertEquals(true, poolDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource")));
    assertEquals(true, poolDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDataSource")));
    assertEquals(true, poolDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource")));
    assertEquals(true, poolDS.isWrapperFor(Class.forName("javax.sql.CommonDataSource")));
    ISQLServerDataSource ids2 = (ISQLServerDataSource) (poolDS.unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource")));
    ids2.setApplicationName("AppName");
    SQLServerXADataSource xaDS = new SQLServerXADataSource();
    assertEquals(true, xaDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource")));
    assertEquals(true, xaDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDataSource")));
    assertEquals(true, xaDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource")));
    assertEquals(true, xaDS.isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerXADataSource")));
    assertEquals(true, xaDS.isWrapperFor(Class.forName("javax.sql.CommonDataSource")));
    ISQLServerDataSource ids3 = (ISQLServerDataSource) (xaDS.unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerDataSource")));
    ids3.setApplicationName("AppName");
}
Also used : SQLServerConnectionPoolDataSource(com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource) ISQLServerDataSource(com.microsoft.sqlserver.jdbc.ISQLServerDataSource) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) ISQLServerDataSource(com.microsoft.sqlserver.jdbc.ISQLServerDataSource) SQLServerXADataSource(com.microsoft.sqlserver.jdbc.SQLServerXADataSource) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Aggregations

SQLServerDataSource (com.microsoft.sqlserver.jdbc.SQLServerDataSource)26 Test (org.junit.jupiter.api.Test)22 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)19 Connection (java.sql.Connection)19 SQLException (java.sql.SQLException)14 SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)9 ISQLServerConnection (com.microsoft.sqlserver.jdbc.ISQLServerConnection)7 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)7 Statement (java.sql.Statement)7 PooledConnection (javax.sql.PooledConnection)7 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)6 ISQLServerDataSource (com.microsoft.sqlserver.jdbc.ISQLServerDataSource)5 ResultSet (java.sql.ResultSet)5 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 CallableStatement (java.sql.CallableStatement)3 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutput (java.io.ObjectOutput)2 ObjectOutputStream (java.io.ObjectOutputStream)2