多线程环境下事务如何组织OrientDB DAO?
How to organize OrientDB DAO in case of transactions in multithreaded environment?
我尝试用 OrientDB 组织我的 DAO 的架构。
下面-我的例子:
连接管理器:
public class DB {
private static final OPartitionedDatabasePoolFactory poolFactory = new OPartitionedDatabasePoolFactory();
public static ODatabaseDocumentTx fromPool() {
return poolFactory.get(sDbUrl, sDbUser, sDbPassword).acquire();
}
}
Dao(多线程环境下使用):
public class Dao {
public static void addGold(String rid, long gold) {
try (ODatabaseDocumentTx db = DB.fromPool()) {
final String updateQuery = "UPDATE " + rid + " INCREMENT gold = " + gold + " LOCK RECORD";
db.command(new OCommandSQL(updateQuery)).execute();
}
}
public static void removeGold(String rid, long gold) {
try (ODatabaseDocumentTx db = DB.fromPool()) {
final String updateQuery = "UPDATE " + rid + " INCREMENT gold = " + -gold + " LOCK RECORD";
db.command(new OCommandSQL(updateQuery)).execute();
}
}
public static String transferGold(String fromRid, String toRid, long gold) {
try (ODatabaseDocumentTx db = DB.fromPool()) {
int numTries = 100;
while (true) {
try {
db.begin(OTransaction.TXTYPE.OPTIMISTIC);
removeGold(fromRid, gold);
addGold(toRid, gold);
db.commit();
return "ok";
} catch (OConcurrentModificationException e) {
db.rollback();
if (--numTries == 0) {
return "error";
}
}
}
}
}
}
交易时从池中获取连接是否有效?那么同一个数据库实例将在同一个线程中 returns?
欢迎任何其他建议
使用 OPartitionedDatabasePool 应该是线程安全的,并提供一种获取连接的方法。我认为仅当您想创建多个 OPartitionedDatabasePool 实例时才使用 OPartitionedDatabasePoolFactory。
OPartitionedDatabasePool 的静态实例:
OPartitionedDatabasePool myPool = new OPartitionedDatabasePool(dbUrl, user, password);
然后只要线程需要连接:
ODatabaseDocumentTx dbConnection = MyClass.myPool.acquire();
(如您使用 OPartitionedDatabasePoolFactory 的代码)。
池应自动处理获取将在您的线程上运行的数据库连接。
我尝试用 OrientDB 组织我的 DAO 的架构。
下面-我的例子:
连接管理器:
public class DB {
private static final OPartitionedDatabasePoolFactory poolFactory = new OPartitionedDatabasePoolFactory();
public static ODatabaseDocumentTx fromPool() {
return poolFactory.get(sDbUrl, sDbUser, sDbPassword).acquire();
}
}
Dao(多线程环境下使用):
public class Dao {
public static void addGold(String rid, long gold) {
try (ODatabaseDocumentTx db = DB.fromPool()) {
final String updateQuery = "UPDATE " + rid + " INCREMENT gold = " + gold + " LOCK RECORD";
db.command(new OCommandSQL(updateQuery)).execute();
}
}
public static void removeGold(String rid, long gold) {
try (ODatabaseDocumentTx db = DB.fromPool()) {
final String updateQuery = "UPDATE " + rid + " INCREMENT gold = " + -gold + " LOCK RECORD";
db.command(new OCommandSQL(updateQuery)).execute();
}
}
public static String transferGold(String fromRid, String toRid, long gold) {
try (ODatabaseDocumentTx db = DB.fromPool()) {
int numTries = 100;
while (true) {
try {
db.begin(OTransaction.TXTYPE.OPTIMISTIC);
removeGold(fromRid, gold);
addGold(toRid, gold);
db.commit();
return "ok";
} catch (OConcurrentModificationException e) {
db.rollback();
if (--numTries == 0) {
return "error";
}
}
}
}
}
}
交易时从池中获取连接是否有效?那么同一个数据库实例将在同一个线程中 returns?
欢迎任何其他建议
使用 OPartitionedDatabasePool 应该是线程安全的,并提供一种获取连接的方法。我认为仅当您想创建多个 OPartitionedDatabasePool 实例时才使用 OPartitionedDatabasePoolFactory。
OPartitionedDatabasePool 的静态实例:
OPartitionedDatabasePool myPool = new OPartitionedDatabasePool(dbUrl, user, password);
然后只要线程需要连接:
ODatabaseDocumentTx dbConnection = MyClass.myPool.acquire();
(如您使用 OPartitionedDatabasePoolFactory 的代码)。
池应自动处理获取将在您的线程上运行的数据库连接。