Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Webapps Synchronization vs No Sync  RSS feed

Peter Neu
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

up to now I was thinking it is a really bad habit do use
synchronization in Servlet Container applications. But yesterday
I performed some JUnitPerf tests on my application.

I use an Oracle Database and the class which communicates directly
with the database has functions like this:

public synchronized HashMap loadPr()throws SQLException
{ //Query something..

I performed seperate tests with the "synchronized" key word and without.
With a varying amount of concurrent users.
Here are the results(the time refers to the wait for the last user):

// 230 Users max duration for no sync 72,2s
// 230 Users max duration for sync 21,4 s

// 150 Users no sync 50,142 s
// 150 Users sync 15,612 s

// 40 Users no sync 12,63 s
// 40 Users sync 5,107 s

// 10 Users no sync 4,676 s
// 10 Users sync 2,554 s

Can anyone tell me why the synchronized methods work faster?
I can't make any sense of it.
Ilja Preuss
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome at the Ranch, Peter!

Perhaps your database becomes really slow with concurrent access? What database are you using? What does the access look like?

To find out about database performance, you can use
Peter Neu
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ilja ,

the database I use is Oracle 9i. Ten concurrent users should be
ok. I was looking over the code of the JUnitPerf test with some prof.
of mine today and we could not not make heads or tails out of
the akward result we got from the test. But we found some addional hints.

The problem seems to be the scheduler not the DB. When 10 users access the application ten instances of the databaseManagement class are created.
The difference between the two approaches is:

In case A (with sync keyword) one object is instantiated and its query
is processed. Same for the others.

In case B (without sync keyword) all ten objects are created at once and then each query is performed. This leads to performance issues.

I use a ConnectionPool which has 16 Connections at its disposal.
Access looks like this (simplified without Exception handling) :

public synchronized void loadHf(int ID_PR) throws SQLException
Connection c;
ResultSet r;
// gets Conn from DataSource
c = ServiceLocator.getInstance().getDBConn();

String sql = "....." ;

final PreparedStatement statement = c.prepareStatement(sql);

statement.setInt(1, ID_PR);

r= statement.executeQuery();

while (

For the test I issue four queries for each user. Hope this clears things up.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!