Esempio n. 1
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 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();
  }
  @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();
  }
  @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();
 }