Search in sources :

Example 1 with LocalTransaction

use of javax.resource.spi.LocalTransaction 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 LocalTransaction

use of javax.resource.spi.LocalTransaction in project jaybird by FirebirdSQL.

the class TestFBResultSet method testExecutableProcedure.

@Test
public void testExecutableProcedure() throws Exception {
    FBManagedConnectionFactory mcf = initMcf();
    DataSource ds = (DataSource) mcf.createConnectionFactory();
    FBConnection c = (FBConnection) ds.getConnection();
    Statement s = c.createStatement();
    LocalTransaction t = c.getLocalTransaction();
    t.begin();
    try {
        s.execute("DROP PROCEDURE testproc");
    } catch (Exception e) {
    }
    t.commit();
    t.begin();
    s.execute(CREATE_PROCEDURE);
    t.commit();
    t.begin();
    CallableStatement p = c.prepareCall("EXECUTE PROCEDURE testproc(?)");
    p.setInt(1, 5);
    assertTrue("execute returned false for execute procedure statement", p.execute());
    assertEquals("wrong answer from sp invocation", 5, p.getInt(1));
    p.close();
    t.commit();
    t.begin();
    s.execute("DROP PROCEDURE testproc");
    s.close();
    t.commit();
    c.close();
}
Also used : LocalTransaction(javax.resource.spi.LocalTransaction) FBConnection(org.firebirdsql.jdbc.FBConnection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 3 with LocalTransaction

use of javax.resource.spi.LocalTransaction in project jaybird by FirebirdSQL.

the class TestFBResultSet method testUseResultSetWithPreparedStatement.

@Test
public void testUseResultSetWithPreparedStatement() 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("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"));
    }
    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 4 with LocalTransaction

use of javax.resource.spi.LocalTransaction in project jaybird by FirebirdSQL.

the class TestFBResultSet method testUseResultSetWithCount.

@Test
public void testUseResultSetWithCount() 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(" CREATE TABLE Customer (name VARCHAR(256),accounts VARCHAR(2000),id VARCHAR(256))");
    } catch (Exception e) {
        ex = e;
    }
    t.commit();
    t.begin();
    PreparedStatement p = c.prepareStatement("SELECT COUNT(*) FROM Customer WHERE id=? AND name=?");
    p.setString(1, "1");
    p.setString(2, "First Customer");
    assertTrue("execute returned false for select statement", p.execute());
    ResultSet rs = p.getResultSet();
    while (rs.next()) {
        if (log != null)
            log.info("count: " + rs.getInt(1));
    }
    p.close();
    t.commit();
    t.begin();
    s.execute("DROP TABLE Customer");
    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)

Aggregations

LocalTransaction (javax.resource.spi.LocalTransaction)4 DataSource (javax.sql.DataSource)4 FBConnection (org.firebirdsql.jdbc.FBConnection)4 Test (org.junit.Test)4