java - JPA multithreaded inserts -


i using ms sql server, , program started losing db connection randomly. using non-xa driver.

the suspect asynchronous database logging added.
sneaky thing is, have used thread pool:

executorservice ruleloggingexecutor = executors.newfixedthreadpool(10); 

and in block of process, start off new thread calls down addlogs() method.

the code works hours, days, , during totally unrelated query, lose db connection. have inkling problem 2 concurrent inserts being attempted. don't know if putting 'synchronized' on addlogs method fix it, or if need transactional code, or what. advice?


in dao:

private entitymanager getentitymanager(initialcontext context) {     try {         if (emf == null) {             emf = (entitymanagerfactory) context                     .lookup("java:jboss/persistence/db");         }          return emf.createentitymanager();     } catch (exception e) {         logger.error(                 "error finding entitymanagerfactory in jndi: "                         + e.getmessage(), e);         return null;     } }    public void addlogs(initialcontext context, string key, string logs,         string responsexml) {     entitymanager em = getentitymanager(context);      try {         tblrulelog log = new tblrulelog();         log.setauthkey(key);         log.setlogmessage(logs);         log.setdatetime(new timestamp(new date().gettime()));         log.setresponsexml(responsexml);          em.persist(log);         em.flush();      } catch (exception e) {         logger.error(e.getmessage(), e);     } {         em.close();     } } 

it seems connection closed after timeout, perhaps due transaction not being commited/rolled (and locks not being released on tables/rows).
manual flushing looks suspicious. i'd use entitymanager.gettransaction().begin/commit() , remove em.flush().


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -