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" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <[EMAIL PROTECTED]>
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:[EMAIL PROTECTED] 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]







--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to