Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dilemma in using DB Connection

 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all java lovers,
I'm developing a job which has two imp classes 1) Main class 2) Shared items (Which is common to all jobs).

In main class i've created instance db connection. and while accessing a method in SharedItems i've created DB connection and closed it before returning.
The problem is if i try to call method 100 times. its is getting damm slow because of db connection creation everytime .!.I'm trying to overcome this and i've figured out few options.

1) Common connection object to entire job like implementing singleton Design pattern
2) static connection for SharedItems(I'm scared to this because of static reference)
3) Singleton pattern for Shareditems.

Which one is better option. and suggest if any other .!

Thanks in advance
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say that the most common (though not necessarily the best in all cases) solution for this situation is the connection pool. The best thing about the pool is that you still close connections (by calling Connection.close()), so you don't have to modify your existing code except for the part which obtains the collection. Closing a connection obtained from the pool doesn't close it physically, but keeps it for later reuse; removing the need to create a new physical connection over and over again. There are several connection pool implementations available (Google).
 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:I'd say that the most common (though not necessarily the best in all cases) solution for this situation is the connection pool. The best thing about the pool is that you still close connections (by calling Connection.close()), so you don't have to modify your existing code except for the part which obtains the collection. Closing a connection obtained from the pool doesn't close it physically, but keeps it for later reuse; removing the need to create a new physical connection over and over again. There are several connection pool implementations available (Google).


hi martin,

I shouldn't use any external libraries for creating connection pooling. it's a restriction. Creating my connection obviously leads to more bugs and i can't spend much time on it as time is constraint . and wat's your comment on implementing on the lines of singleton ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic