@Test
  public void testUnCachedPrepared() throws Exception {
    BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager();
    tm.setTransactionTimeout(60);
    tm.begin();

    Connection connection = poolingDataSource2.getConnection();

    PreparedStatement prepareStatement1 =
        connection.prepareStatement("SELECT 1 FROM nothing WHERE a=? AND b=? AND c=? AND d=?");
    PreparedStatement prepareStatement2 =
        connection.prepareStatement("SELECT 1 FROM nothing WHERE a=? AND b=? AND c=? AND d=?");

    Assert.assertNotSame(prepareStatement1, prepareStatement2);

    prepareStatement2.close();

    prepareStatement2 =
        connection.prepareStatement("SELECT 1 FROM nothing WHERE a=? AND b=? AND c=? AND d=?");
    Assert.assertNotSame(prepareStatement1, prepareStatement2);

    prepareStatement1.close();
    prepareStatement2.close();

    connection.close();
    tm.shutdown();
  }
Пример #2
0
  /**
   * This method should be called in the @After method of a test to clean up the persistence unit
   * and datasource.
   *
   * @param context A HashMap generated by {@link org.drools.persistence.util.PersistenceUtil
   *     setupWithPoolingDataSource(String)}
   */
  public static void cleanUp(HashMap<String, Object> context) {
    if (context != null) {

      BitronixTransactionManager txm = TransactionManagerServices.getTransactionManager();
      if (txm != null) {
        txm.shutdown();
      }

      Object emfObject = context.remove(ENTITY_MANAGER_FACTORY);
      if (emfObject != null) {
        try {
          EntityManagerFactory emf = (EntityManagerFactory) emfObject;
          emf.close();
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }

      Object ds1Object = context.remove(DATASOURCE);
      if (ds1Object != null) {
        try {
          PoolingDataSource ds1 = (PoolingDataSource) ds1Object;
          ds1.close();
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    }
  }
  @Test
  public void testSetters() throws Exception {
    BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager();
    tm.setTransactionTimeout(30);
    tm.begin();

    Connection connection = poolingDataSource1.getConnection();

    long start = System.nanoTime();
    PreparedStatement stmt =
        connection.prepareStatement("SELECT 1 FROM nothing WHERE a=? AND b=? AND c=? AND d=?");
    Date date = new Date(0);
    for (int i = 0; i < 50000; i++) {
      stmt.setString(1, "foo");
      stmt.setInt(2, 999);
      stmt.setDate(3, date);
      stmt.setFloat(4, 9.99f);
      stmt.clearParameters();
    }
    long totalTime = System.nanoTime() - start;

    stmt.executeQuery();

    connection.close();

    tm.commit();
    tm.shutdown();
  }
  @After
  public void tearDown() throws Exception {
    BitronixTransactionManager txm = TransactionManagerServices.getTransactionManager();
    assertTrue("There is still a transaction running!", txm.getCurrentTransaction() == null);

    cleanUp(context);
    logService.dispose();
  }
  public void testDelistErrorAndUnilateralRollbackOnCommit() throws Exception {
    btm.begin();

    Connection connection1 = poolingDataSource1.getConnection();
    JdbcConnectionHandle handle1 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection1);
    XAConnection xaConnection1 =
        (XAConnection) AbstractMockJdbcTest.getWrappedXAConnectionOf(handle1.getPooledConnection());
    MockXAResource xaResource1 = (MockXAResource) xaConnection1.getXAResource();
    xaResource1.setEndException(
        new BitronixXAException("screw delistment", XAException.XAER_RMERR));
    xaResource1.setRollbackException(
        new BitronixXAException("delistment was screwed, cannot rollback", XAException.XAER_RMERR));

    connection1.createStatement(); // triggers enlistment

    Connection connection2 = poolingDataSource2.getConnection();
    JdbcConnectionHandle handle2 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection2);
    XAConnection xaConnection2 =
        (XAConnection) AbstractMockJdbcTest.getWrappedXAConnectionOf(handle2.getPooledConnection());
    MockXAResource xaResource2 = (MockXAResource) xaConnection2.getXAResource();
    xaResource2.setEndException(
        new BitronixXAException("what was that transaction again?", XAException.XAER_NOTA));
    xaResource2.setRollbackException(
        new BitronixXAException(
            "delistment unilaterally rolled back, cannot rollback twice", XAException.XAER_RMERR));

    connection2.createStatement(); // triggers enlistment

    try {
      btm.commit();
      fail("expected RollbackException");
    } catch (RollbackException ex) {
      assertEquals(
          "delistment error caused transaction rollback"
              + System.getProperty("line.separator")
              + "  resource(s) [pds2] unilaterally rolled back"
              + System.getProperty("line.separator")
              + "  resource(s) [pds1] could not be delisted",
          ex.getMessage());
    }

    // check flow
    List orderedEvents = EventRecorder.getOrderedEvents();
    log.info(EventRecorder.dumpToString());

    assertEquals(8, orderedEvents.size());
    int i = 0;
    assertEquals(Status.STATUS_ACTIVE, ((JournalLogEvent) orderedEvents.get(i++)).getStatus());
    assertEquals(XAResource.TMNOFLAGS, ((XAResourceStartEvent) orderedEvents.get(i++)).getFlag());
    assertEquals(XAResource.TMNOFLAGS, ((XAResourceStartEvent) orderedEvents.get(i++)).getFlag());
    assertEquals(XAResource.TMSUCCESS, ((XAResourceEndEvent) orderedEvents.get(i++)).getFlag());
    assertEquals(
        Status.STATUS_MARKED_ROLLBACK, ((JournalLogEvent) orderedEvents.get(i++)).getStatus());
    assertEquals(XAResource.TMSUCCESS, ((XAResourceEndEvent) orderedEvents.get(i++)).getFlag());
    assertEquals(
        Status.STATUS_ROLLING_BACK, ((JournalLogEvent) orderedEvents.get(i++)).getStatus());
    assertEquals(Status.STATUS_ROLLEDBACK, ((JournalLogEvent) orderedEvents.get(i++)).getStatus());
  }
  @Test
  public void testPrepares() throws Exception {
    BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager();
    tm.setTransactionTimeout(60);
    tm.begin();

    Connection connection = poolingDataSource2.getConnection();

    for (int i = 0; i < 1000; i++) {
      PreparedStatement prepareStatement =
          connection.prepareStatement("SELECT 1 FROM nothing WHERE a=? AND b=? AND c=? AND d=?");
      prepareStatement.close();
    }

    connection.close();

    tm.commit();
    tm.shutdown();
  }
 protected void tearDown() throws Exception {
   poolingDataSource1.close();
   poolingDataSource2.close();
   btm.shutdown();
 }