ORMLite + 交易

ORMLite + Transactions

我无法理解 DaoManager 的默认行为。

DaoManager.createDao(connectionSource, theClass);

这需要一个 connectionSource - 而不是一个连接。 因此,如果我执行以下操作:

TransactionManager.callInTransaction(
    localConnection,
    connectionSource.getDatabaseType(),
    new Callable<Void>() {
        public Void call() throws Exception {
            dao.create(user);
            dao.create(player);
                return null;
            }
    });

事务应限于单个连接(localConnection)。 DAO 是如何处理的?还是根本不处理?

The transaction should be limited to a single connection (the localConnection). How does the DAO handle that? Or does it not handle it at all?

嗯。我不是 100% 确定为什么会公开连接方法。我要弃用它。

您确实应该使用 dao.callBatchTasks(...) method. If you need to use the TransactionManager directly, I'd use the method that takes a ConnectionSource而不是 连接。

您可以查看 source of TransactionManager.callInTransaction(...) 以了解它正在保存连接,稍后 DAO 通过一些 ORMLite 魔法使用该连接。因此,在已保存的连接上禁用自动提交,然后在批处理任务完成后恢复。

我分析了代码库,似乎如果我最初在内部请求 dao 时使用相同的 ConnectionSource,则会建立一个 "savedConnection",然后强制使用它。

如果有人可以 confirm/refute 这个 - 或者知道如何完全正确地做到这一点 - 请告诉我!