database - Java more than one DB connection in UserTransaction -


static void clean() throws exception {   final usertransaction tx = initialcontext.dolookup("usertransaction");   tx.begin();    try {     final datasource ds = initialcontext.dolookup(databases.admin);     connection connection1 = ds.getconnection();     connection connection2 = ds.getconnection();     preparedstatement st1 = connection1.preparestatement("xxx delete records xxx"); // delete data      preparedstatement st2 = connection2.preparestatement("xxx insert records xxx"); // insert new data same primary deleted data above      st1.executeupdate();     st1.close();     connection1.close();     st2.executeupdate();     st2.close();     connection2.close();     tx.commit();   } {     if (tx.getstatus() == status.status_active) {       tx.rollback();     }   } } 

i have web app, dao taking datasource object create individual connection perform database operations.

so have usertransaction, inside there 2 dao object doing separated action, first 1 doing deletion , second 1 doing insertion. deletion delete records allow insertion take place because insertion insert same primary key's data.

i take out dao layer , translate logic code above. there 1 thing couldn't understand, based on code above, insertion operation should fail, because code (inside usertransaction) take 2 different connections, don't know each other, , first deletion haven't committed obviously, second statement (insertion) should fail (due unique constraint), because 2 database operation not in same connection, second connection not able detect uncommitted changes. amazingly, doesn't fail, , both statement can work perfectly.

can explain this? configuration can done achieve result? or whether understanding wrong?

since application running in weblogic server, java-ee-container managing transaction , connection you. if call datasource#getconnection multiple times in java-ee transaction, multiple connection instances joining same transaction. connections connect database identical session. using oracle can check following snippet in @stateless ejb:

@resource(lookup="jdbc/myds") private datasource ds;  @transactionattribute(transactionattributetype.requires_new) @schedule(hour="*", minute="*", second="42") public void testdatasource() throws sqlexception {      try ( connection con1 = ds.getconnection();           connection con2 = ds.getconnection();         ) {          string sessid1 = null, sessid2 = null;         try (resultset rs1 = con1.createstatement().executequery("select userenv('sessionid') dual") ){             if ( rs1.next() ) sessid1 = rs1.getstring(1);         };         try (resultset rs2 = con2.createstatement().executequery("select userenv('sessionid') dual") ){             if ( rs2.next() ) sessid2 = rs2.getstring(1);         };          log.log( level.info," con1={0}, con2={1}, sessid1={2}, sessid2={3}"                , new object[]{ con1, con2, sessid1, sessid2}                );     }  } 

this results in following log-message:

con1=com.sun.gjc.spi.jdbc40.connectionwrapper40@19f32aa,  con2=com.sun.gjc.spi.jdbc40.connectionwrapper40@1cb42e0,  sessid1=9347407,  sessid2=9347407 

note different connection instances same session-id.

for more details see eg this question


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -