tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rodrigo Ruiz <rr...@gridsystems.com>
Subject Re: [OT] Synchronising database access
Date Fri, 21 Nov 2003 12:18:24 GMT
If you implement your database access as a static synchronized method, 
only one instance of the class will be able to execute it at the same time.
If you put all your database stuff in the same class, this schema should 
be enough. If only one servlet does database access, then you can put 
these methods on the servlet, but if you have more than one servlet 
doing this stuff, you should place your code in a separated class.

Apart from this, take into account that this will be a, generally 
speaking, very weak solution. In a general scenario, other applications 
(or webapps) could be accessing your database. In those cases, the only 
safe solution are database transactions :-)

HTH,
Rodrigo Ruiz

Antony Paul wrote:

>thanks for the reply
>synchrosing servlet code dont work ?
>
>Antony Paul
>
>----- Original Message -----
>From: "Peter Guyatt" <pguyatt@telesoft-technologies.com>
>To: "Tomcat Users List" <tomcat-user@jakarta.apache.org>
>Sent: Friday, November 21, 2003 5:28 PM
>Subject: RE: [OT] Synchronising database access
>
>
>  
>
>>Hi There,
>>
>>Use a singleton class to manage the database transactions.
>>
>>Then implement a owner mechanism. E.g.
>>
>>class DbTxMgr {
>>
>>String owner = null;
>>
>>public synchronized boolean lockMgr (String threadId) {
>>if (owner == null) {
>>owner = threadId;
>>return true;
>>} else {
>>if (owner.equals(threadId)) {
>>return true
>>}
>>return false;
>>}
>>}
>>
>>public synchronized void releaseMgr (String threadId) {
>>if (owner != null) {
>>if (owner.equals(threadId)) {
>>owner = null;
>>}
>>}
>>}
>>
>>public synchronized boolean insert (String statement, String threadId) {
>>if (owner == null) {
>>owner = threadId;
>>}
>>if (owner.equals(threadId)) {
>>file://do the transaction
>>}
>>}
>>}
>>
>>your code
>>
>>class tester {
>>file://name of your thread
>>Thread t = new Thread("testerThread")
>>...
>>
>>if (lockMgr(t.getName())) {
>>file://do database code
>>}
>>lockMgr.releaseLock(t.getName());
>>
>>}
>>
>>This way olny the owner of the lock can perform transactions and all other
>>threads must wait for the lock to be released
>>
>>Thanks
>>
>>Pete
>>
>>
>>-----Original Message-----
>>From: Antony Paul [mailto:antonypaul24@hotmail.com]
>>Sent: 21 November 2003 11:51
>>To: Tomcat Users List
>>Subject: [OT] Synchronising database access
>>
>>
>>Hi,
>>    I want to synchronise all database access. There are lots of
>>    
>>
>situations
>  
>
>>where first first a select query is performed to get the results and then
>>insert/update data. I dont want to implement row level locking or
>>    
>>
>optimistic
>  
>
>>locking. I just want to synchronise the whole process. Only after a thread
>>completes the entire process other threads can execute the code. How to do
>>it. Do I have to synchronise on Connection or on this or implement
>>SingleThreadModel. I also want to know how much extra time a synchronised
>>block requires than an unsynchronised block.
>>
>>Antony Paul.
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>
>>    
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message