Which Way to Cope with Concurrent Access?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   baris_aksu
Posted On:   Wednesday, June 30, 2004 01:47 AM

Hi all, I want to know whether there is any difference between the following scenarios in a multi-threaded environment : Say we have a function aMethod() that can be executed by more than one thread at a time in a class whose pseudo-code is something like : code: ------------------------ public class Test { public static void main(String[] args) { aMethod(); } static void aMethod() { Connection con = get_A_Connection(); con.setAutoCommit(false); try { insert_into(table_A); delete_form(table_b); con.commit(); } catch(Exception ex) { con.rollback(); } } }    More>>

Hi all,

I want to know whether there is any difference between the following scenarios in a multi-threaded environment :

Say we have a function aMethod() that can be executed by more than one thread at a time in a class whose pseudo-code is something like :


code:

------------------------

			
public class Test {
public static void main(String[] args) {
aMethod();
}
static void aMethod() {
Connection con = get_A_Connection();
con.setAutoCommit(false);
try {
insert_into(table_A);
delete_form(table_b);
con.commit();
}
catch(Exception ex) {
con.rollback();
}
}
}




Now, AFAIK insert and delete operations in aMethod() requires obtaining exclusive locks on the respective tables, so no one thread can ever insert into table A before its lock is released by the owning thread with a commit or rollback; effectively there is always only one thread executing inside aMethod() ant the other threads are waiting for the locks to be released. Am i right?

Is this situation the same with the following scenario, where access to aMethod() is synchronized? Here, still no two threads can be in aMethod() simultaneously, right?


code:

-----------------------

			
public class Test {
static Object syncObj = new Object();
public static void main(String[] args) {
synchronized(syncObj) {
aMethod();
}
}
static void aMethod() {
Connection con = get_A_Connection();
con.setAutoCommit(true);
try {
insert_into(table_A);
delete_form(table_b);
}
catch(Exception ex) {}
}
}




-------------------------------


Thanks in advance...    <<Less

Re: Which Way to Cope with Concurrent Access?

Posted By:   Sean_Owen  
Posted On:   Wednesday, June 30, 2004 08:10 AM

Are you talking about Java threads or database threads? In your examples, there is always only one thread running, it appears, from main.



OK, so assume you mean that multiple Java threads call aMethod(). In the first example, no, there is nothing that prevents multiple Java threads from executing in aMethod() simultaneously. One thread's call to JDBC methods may block while another thread's work finishes in the database, but that is not directly due to synchronization. In the second case, still nothing prevents two threads from executing in aMethod() since it is not synchronized.



What goes on in the database in terms of threads and locks depends on the database and also the transaction isolation level that you have specified. It is possible that one transaction must wait for the other completely, but also possible that both can execute simultaneously.

About | Sitemap | Contact