How to organize OrientDB DAO in case of transactions in multithreaded environment? -


i try organize architecture of dao orientdb.
below - example of my:

connection manager:

public class db {      private static final opartitioneddatabasepoolfactory poolfactory = new opartitioneddatabasepoolfactory();      public static odatabasedocumenttx frompool() {         return poolfactory.get(sdburl, sdbuser, sdbpassword).acquire();     } } 

dao (it uses in multithreaded environment):

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";                     }                 }             }         }     } } 

it's valid connection pool in case of transaction? same database instance returns in same thread?

any other advice welcome

using opartitioneddatabasepool should thread safe , provide way connections. think opartitioneddatabasepoolfactory used if want create multiple opartitioneddatabasepool instances.

a static instance of opartitioneddatabasepool:

opartitioneddatabasepool mypool = new opartitioneddatabasepool(dburl, user, password); 

and anytime thread needs connection:

odatabasedocumenttx dbconnection = myclass.mypool.acquire(); 

(as in code opartitioneddatabasepoolfactory).

the pool should automatically handle getting database connection function on thread.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -