使用带有JDBI / IDBI / Dropwizard的事务 - 回滚问题

时间:2013-12-27 04:56:49

标签: java mysql transactions dropwizard jdbi

我在使用IDBI处理事务时遇到了很多麻烦。我们正在使用dropwizard框架,简单的插入,更新,选择和删除已找到工作,但现在我们似乎无法使事务正常工作。这是我正在尝试的

public class JDb {
    private JustinTest2 jTest2 = null;
    private Handle dbHandle = null;

    public JDb(final IDBI idbi) {
        try {
            dbHandle = idbi.open();
            dbHandle.getConnection().setAutoCommit(false);
            jTest2 = dbHandle.attach(JustinTest2.class);
        } catch( SQLException e ) {

        }
    }

   public void writeJustin(final int styleId, final int eventId) {
        dbHandle.begin();
        int num = jTest2.findByStyleId(styleId);

        try {
            jTest2.doStuff(styleId, eventId);
            dbHandle.commit();
        } catch(Exception e) {
            dbHandle.rollback(); // Never rolls back here, always get the inserted row!
        }

        num = jTest2.findByStyleId(styleId); 
   } 
}

这是我的JustinTest2课程

public abstract class JustinTest2 {

    @SqlUpdate("INSERT INTO jTest2 (styleId, jNum) VALUES (:styleId, :jNum)")
    public abstract void insert(@Bind("styleId") int styleId, @Bind("jNum") int jNum);

    @SqlQuery("SELECT count(styleId) " +
            "FROM jTest2 " +
            "WHERE styleId=:styleId")
    public abstract int findByStyleId(@Bind("styleId") int styleId);


    public int doStuff(int styleId, int eventId) throws Exception{
        int count = findByStyleId(styleId);

        insert(styleId, eventId);

        count = findByStyleId(styleId);

        if(count==1) {
            throw new Exception("Roll back");
        }

        return count;
    }
}

我也尝试过像writeJustin一样实现:

public void writeJustin(final int styleId, final int eventId) throws Exception {
    int rows_updated = jTest2.inTransaction(new Transaction<Integer, JustinTest2>() {
        @Override
        public Integer inTransaction(JustinTest2 transactional, TransactionStatus status) throws Exception {

            jTest2.insert(styleId, eventId);
            int num = transactional.findByStyleId(styleId);

            try {
                if(num == 1) throw new Exception("BOOM");    
            } catch (Exception e) {
                transactional.rollback();
                throw e;
            }

            num = transactional.findByStyleId(styleId);
            return num;
        }
    });
}

我似乎无法让事务回滚,在每种方式中插入的行总是在回滚后存在,无论我是直接尝试通过句柄还是我使用inTransaction(我的理解不应该提交事务)如果在回调中抛出异常)任何人都知道我可能做错了什么?

2 个答案:

答案 0 :(得分:27)

这与您的问题相关,但我将其添加为答案,因为您的问题在Google搜索结果中很高,并且没有很多例子。

使用JDBI v2,您可以使用@Transaction annotation来简化代码。只需使用注释修饰public方法,JDBI将在后台处理begin,commit和rollback。

public abstract class JustinTest2 {

    @SqlUpdate("INSERT INTO jTest2 (styleId, jNum) VALUES (:styleId, :jNum)")
    protected abstract void insert(@Bind("styleId") int styleId, @Bind("jNum") int jNum);

    @SqlQuery("SELECT count(styleId) " +
            "FROM jTest2 " +
            "WHERE styleId=:styleId")
    protected abstract int findByStyleId(@Bind("styleId") int styleId);

    @Transaction
    public int doStuff(int styleId, int eventId) throws Exception{
        int count = findByStyleId(styleId);

        insert(styleId, eventId);

        count = findByStyleId(styleId);

        if(count==1) {
            throw new Exception("Roll back");
        }

        return count;
    }
}

请注意,我保护了insertfindByStyleId方法;从public开始强制执行它们在一个事务中一起完成(在公共doStuff方法中);不是private因为JDBI自动生成的实现无法覆盖它们(因为private abstract方法不能正常工作 - 你要强制编译器接受没有身体的方法。

您还可以在注释中指定TransactionIsolationLevel以覆盖数据库的默认值。

@Transaction(TransactionIsolationLevel.REPEATABLE_READ)

答案 1 :(得分:9)

我想出来了。事实证明我正在测试的表是使用MyISAM而不是InnoDB作为存储引擎。 MyISAM不支持交易。我使用InnoDB重建了表,上面的代码运行正常。

对于任何不知道的人,您可以使用以下方式查看表正在使用的引擎:

show create table <tablename>;

应该看到类似的内容:

CREATE TABLE `grades` (
    `id` int(11) NOT NULL,
    `percent` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1