jeudi 29 octobre 2015

How to skip code for testing or convert oracle syntax to h2

My team and I are working with oracle database for development and production. But to save time, we decided to use H2 database for testing because it is fast.

Now we run into a problem related to SQL grammar.

public void lock(Collection<String> locks) {
    if (locks== null || locks.size() == 0)
        return;

    Connection dbc = null;
    Statement st = null;
    String q = null;
    try {
        dbc = db.getConnection();
        dbc.setAutoCommit(false);
        st = dbc.createStatement();
        st.setFetchSize(fetchSize);
        q = "LOCK TABLE resource_lock IN EXCLUSIVE MODE";
        st.executeUpdate(q);

        // Update the lock info in DB
        List idLists = DbUtil.splitIdList(locks);
        Iterator i = idLists.iterator();
        while (i.hasNext()) {
            List idList = (List) i.next();
            q =
                "DELETE FROM resource_lock\n" +
                "WHERE " + DbUtil.generateStrIn("lock", idList);
            st.executeUpdate(q);
        }

        st.close();
        st = null;
        dbc.commit();
        dbc.close();
        dbc = null;
    } catch (SQLException e) {
        throw new DbException(e, q);
    } finally {
        DbUtil.cleanup(log, null, st, dbc);
    }
}

The issue was occurred at "LOCK TABLE resource_lock IN EXCLUSIVE MODE" which this is oracle only grammar. In the test I wrote it always throws syntax error which states

Caused by: com.resource.db.DbException: Database Error: Syntax error in SQL statement "LOCK[*] TABLE RESOURCE_LOCK IN EXCLUSIVE MODE "; SQL statement:
LOCK TABLE resource_lock IN EXCLUSIVE MODE [42000-160]
Last SQL was:
LOCK TABLE resource_lock IN EXCLUSIVE MODE

I was wondering if there are syntax for H2 that has equivalent operation. If there isn't any then I was wondering how would I skip this section for testing since many of other classes method uses this to update the database. I would not want the other test to fail because of this.

Aucun commentaire:

Enregistrer un commentaire