Search in sources :

Example 1 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBResultSet method testUsePreparedStatementAcrossTransactions.

@Test
public void testUsePreparedStatementAcrossTransactions() throws Exception {
    FBManagedConnectionFactory mcf = initMcf();
    DataSource ds = (DataSource) mcf.createConnectionFactory();
    FBConnection c = (FBConnection) ds.getConnection();
    Statement s = c.createStatement();
    LocalTransaction t = c.getLocalTransaction();
    Exception ex = null;
    t.begin();
    try {
        s.execute("DROP TABLE T1");
    } catch (Exception e) {
    }
    t.commit();
    t.begin();
    try {
        s.execute("CREATE TABLE T1 ( C1 INTEGER not null primary key, C2 SMALLINT, C3 DECIMAL(18,0), C4 FLOAT, C5 DOUBLE PRECISION, C6 CHAR(10), C7 VARCHAR(20))");
    } catch (Exception e) {
        ex = e;
    }
    t.commit();
    t.begin();
    PreparedStatement p = c.prepareStatement("insert into T1 values (?, ?, ?, ?, ?, ?, ?)");
    p.setInt(1, 1);
    p.setShort(2, (short) 1);
    p.setLong(3, 1);
    p.setFloat(4, (float) 1.0);
    p.setDouble(5, 1.0);
    p.setString(6, "one");
    p.setString(7, "one");
    assertFalse("execute returned true for insert statement", p.execute());
    p.setInt(1, 2);
    p.setShort(2, (short) 2);
    p.setLong(3, 2);
    p.setFloat(4, (float) 2.0);
    p.setDouble(5, 2.0);
    p.setString(6, "two");
    p.setString(7, "two");
    assertEquals("executeUpdate count != 1", 1, p.executeUpdate());
    p.close();
    p = c.prepareStatement("select * from T1 where C1 = ?");
    p.setInt(1, 1);
    ResultSet rs = p.executeQuery();
    while (rs.next()) {
        if (log != null)
            log.info("C1: " + rs.getInt(1) + " C2: " + rs.getShort(2) + " C3: " + rs.getLong(3) + " C4: " + rs.getFloat(4) + " C5: " + rs.getDouble(5) + " C6: " + rs.getString(6) + " C7: " + rs.getString(7));
        if (log != null)
            log.info("C1: " + rs.getInt("C1") + " C2: " + rs.getShort("C2") + " C3: " + rs.getLong("C3") + " C4: " + rs.getFloat("C4") + " C5: " + rs.getDouble("C5") + " C6: " + rs.getString("C6") + " C7: " + rs.getString("C7"));
    }
    t.commit();
    // does prepared statement persist across transactions?
    t.begin();
    p.setInt(1, 2);
    rs = p.executeQuery();
    while (rs.next()) {
        if (log != null)
            log.info("C1: " + rs.getInt(1) + " C2: " + rs.getShort(2) + " C3: " + rs.getLong(3) + " C4: " + rs.getFloat(4) + " C5: " + rs.getDouble(5) + " C6: " + rs.getString(6) + " C7: " + rs.getString(7));
        if (log != null)
            log.info("C1: " + rs.getInt("C1") + " C2: " + rs.getShort("C2") + " C3: " + rs.getLong("C3") + " C4: " + rs.getFloat("C4") + " C5: " + rs.getDouble("C5") + " C6: " + rs.getString("C6") + " C7: " + rs.getString("C7"));
    }
    p.close();
    t.commit();
    t.begin();
    s.execute("DROP TABLE T1");
    s.close();
    t.commit();
    c.close();
    if (ex != null) {
        throw ex;
    }
}
Also used : LocalTransaction(javax.resource.spi.LocalTransaction) FBConnection(org.firebirdsql.jdbc.FBConnection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 2 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBStandAloneConnectionManager method testUseStatement.

@Test
public void testUseStatement() throws Exception {
    FBManagedConnectionFactory mcf = initMcf();
    DataSource ds = (DataSource) mcf.createConnectionFactory();
    FBConnection c = (FBConnection) ds.getConnection();
    Statement s = c.createStatement();
    FirebirdLocalTransaction t = c.getLocalTransaction();
    assertNotNull("Could not get LocalTransaction", t);
    Exception ex = null;
    t.begin();
    try {
        s.execute("CREATE TABLE T1 ( C1 SMALLINT, C2 SMALLINT)");
    } catch (Exception e) {
        ex = e;
    }
    t.commit();
    t.begin();
    s.execute("DROP TABLE T1");
    s.close();
    t.commit();
    c.close();
    if (ex != null) {
        throw ex;
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection) Statement(java.sql.Statement) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 3 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestBackupManager method testRestorePageSize32768.

/**
 * Test if restoring a database to page size 32768 works.
 */
@Test
public void testRestorePageSize32768() throws Exception {
    assumeTrue("Test requires 32K page size support", getDefaultSupportInfo().supportsPageSize(PageSizeConstants.SIZE_32K));
    usesDatabase.createDefaultDatabase();
    backupManager.backupDatabase();
    backupManager.setRestoreReplace(true);
    backupManager.setRestorePageSize(PageSizeConstants.SIZE_32K);
    backupManager.restoreDatabase();
    try (Connection con = getConnectionViaDriverManager()) {
        GDSHelper gdsHelper = ((FBConnection) con).getGDSHelper();
        final FbDatabase currentDatabase = gdsHelper.getCurrentDatabase();
        final byte[] databaseInfo = currentDatabase.getDatabaseInfo(new byte[] { ISCConstants.isc_info_page_size }, 10);
        assertEquals("Unexpected info item", ISCConstants.isc_info_page_size, databaseInfo[0]);
        int length = iscVaxInteger2(databaseInfo, 1);
        int pageSize = iscVaxInteger(databaseInfo, 3, length);
        assertEquals("Unexpected page size", 32768, pageSize);
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection) Connection(java.sql.Connection) FBConnection(org.firebirdsql.jdbc.FBConnection) GDSHelper(org.firebirdsql.gds.impl.GDSHelper) FbDatabase(org.firebirdsql.gds.ng.FbDatabase) Test(org.junit.Test)

Example 4 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBManager method testSetPageSize_createdDatabaseHasSize.

@Test
public void testSetPageSize_createdDatabaseHasSize() throws Exception {
    FBManager m = createFBManager();
    m.setServer(DB_SERVER_URL);
    m.setPort(DB_SERVER_PORT);
    m.start();
    try {
        // Adding .fdb suffix to prevent conflicts with other tests if drop fails
        final String databasePath = getDatabasePath() + ".fdb";
        m.setPageSize(16384);
        // check create
        m.createDatabase(databasePath, DB_USER, DB_PASSWORD);
        try {
            FBConnection connection = (FBConnection) DriverManager.getConnection(getUrl() + ".fdb", getDefaultPropertiesForConnection());
            try {
                final FbDatabase currentDatabase = connection.getGDSHelper().getCurrentDatabase();
                final byte[] databaseInfo = currentDatabase.getDatabaseInfo(new byte[] { ISCConstants.isc_info_page_size }, 10);
                assertEquals("Unexpected info item", ISCConstants.isc_info_page_size, databaseInfo[0]);
                int length = iscVaxInteger2(databaseInfo, 1);
                int pageSize = iscVaxInteger(databaseInfo, 3, length);
                assertEquals("Unexpected page size", 16384, pageSize);
            } finally {
                connection.close();
            }
        } finally {
            m.dropDatabase(databasePath, DB_USER, DB_PASSWORD);
        }
    } finally {
        m.stop();
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection) FbDatabase(org.firebirdsql.gds.ng.FbDatabase) Test(org.junit.Test)

Example 5 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBManager method testDialect3_dbCreatedWithRightDialect.

@Test
public void testDialect3_dbCreatedWithRightDialect() throws Exception {
    FBManager m = createFBManager();
    m.setServer(DB_SERVER_URL);
    m.setPort(DB_SERVER_PORT);
    m.start();
    try {
        // Adding .fdb suffix to prevent conflicts with other tests if drop fails
        final String databasePath = getDatabasePath() + ".fdb";
        m.setDialect(3);
        // check create
        m.createDatabase(databasePath, DB_USER, DB_PASSWORD);
        try {
            FBConnection connection = (FBConnection) DriverManager.getConnection(getUrl() + ".fdb", getDefaultPropertiesForConnection());
            try {
                final FbDatabase currentDatabase = connection.getGDSHelper().getCurrentDatabase();
                assertEquals("Unexpected database dialect", 3, currentDatabase.getDatabaseDialect());
            } finally {
                connection.close();
            }
        } finally {
            m.dropDatabase(databasePath, DB_USER, DB_PASSWORD);
        }
    } finally {
        m.stop();
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection) FbDatabase(org.firebirdsql.gds.ng.FbDatabase) Test(org.junit.Test)

Aggregations

FBConnection (org.firebirdsql.jdbc.FBConnection)15 Test (org.junit.Test)10 FbDatabase (org.firebirdsql.gds.ng.FbDatabase)6 DataSource (javax.sql.DataSource)5 LocalTransaction (javax.resource.spi.LocalTransaction)4 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 GDSHelper (org.firebirdsql.gds.impl.GDSHelper)2 Statement (java.sql.Statement)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 TransactionParameterBuffer (org.firebirdsql.gds.TransactionParameterBuffer)1 FbTransaction (org.firebirdsql.gds.ng.FbTransaction)1 SQLExceptionChainBuilder (org.firebirdsql.util.SQLExceptionChainBuilder)1