0

I face following issue while trying to write some JBehave BDD test. Scenario is the following: Due to an MQ message some specific records should be saved into my database and some should be deleted from it. In my @When step I send the given message and in my @Then step I have some asserts to controll the result. I face issue with the persist and update methods of Hibernate JPA, because it happens just after my code runs to my @Then step and so I got always false result. When I check it in debug mode, and check every recorsd which should be updated / deteled, they are fine. I assume my asserts should be executed just after the transaction is committed into the database - but this is not the case as I can see. Is there a way to set a delay or sleep time between database transactions?

Here is e.g. my delete method in my main project:

public void deleteByAbcId(final String Id) {
    getEm().createNamedQuery(TABLE.NAMED_QUERY_DELETE_BY_ABC_ID)
            .setParameter(Table.QUERY_PARAM_ABC_ID, Id)
            .executeUpdate();
}

And in my BDD project I set up database connection as follows:

public class DatabaseService implements Closeable {

private EntityManagerFactory emf = null;
private EntityManager em = null;

/**
 * This creates an entity manager based on the db connection parameters received in the argument
 */
public DatabaseService(Properties configuration) {
    emf = Persistence.createEntityManagerFactory("project-pu", configuration);
    em = emf.createEntityManager();
    em.getTransaction().begin();
}

/**
 * Returns the entity manager for the db connection
 */
public EntityManager getEm() {
    return em;
}

In my assertion I use the following query to check if the given record was successfully removed from the db:

assertNull(dbHelper.findTableIdBasedOnAbcId(Serenity.sessionVariableCalled(ABC_ID)));

My dbHelper class looks like as following:

public class DbHelper {

private DatabaseService database;
private Configuration config = Configuration.getInstance();

public DbHelper() {
    database = new DatabaseService(config.getDbProperties());
}

public String findTableIdBasedOnAbcId(String Id) throws Exception {

    String query = "SELECT id FROM TABLE WHERE ABC_ID = ?1";
    Query queryResult = database.getEm().createNativeQuery(query);
    queryResult.setParameter(1, Id);

    List<Long> list = (List<Long>) queryResult.getResultList();

    if (!list.isEmpty()) {
        return String.valueOf(list.get(0));
    }
    return null;
}

It always retunr the given record of the database, which gets not deleted.

This is the persistence xml of my main project:

<persistence-unit name="aaa-pu" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>jdbc/aaaa-ds</jta-data-source>
    <mapping-file>META-INF/orm.xml</mapping-file>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.transaction.jta.platform" value="org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform"/>
        <property name="hibernate.hbm2ddl.auto" value="none"/>
        <property name="hibernate.id.new_generator_mappings" value="false"/>
        <property name="tomee.jpa.factory.lazy" value="true"/>
    </properties>

</persistence-unit>

And this is the one of my BDD project:

<persistence-unit name="project-pu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="hibernate.connection.autocommit" value="false" />
        <property name="hibernate.hbm2ddl.auto" value="none" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.generate_statistics" value="false"/>
    </properties>
</persistence-unit>

Any idea, how to set transactions not to execute the assert method before the executeUpdate of Hibernate runs? I tried to call the entityManager.getTransaction.commit method and then start a new one every time but no change. Is there a way to lock the transaction until the executeUpdate is done and just after that allowing the select query? Or is it possible with JBehave to give a waiting or sleep time between steps?

Thank you.

dorcsi
  • 295
  • 2
  • 6
  • 24

0 Answers0