Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single Database connection

 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to create a single Database connection point that I can use for 4 classes in my Java Web Application.

Here is my ConnectionManager Class:


Now how would I access this in each one of my classes?

For example here is one:



Another Class looks like this:


Please advise.
[ October 17, 2007: Message edited by: Dan Parsons ]
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to pass connection from one class to another,
Instead whenever you need a connection in any of your class just call
ConnectionManager.getConnection() and it will return the active connection.

IF classes are going to be used by more than one threds, make the threadlocal connection inside ConnectionManager.
 
Surasak Leenapongpanit
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to pass connection from one class to another


If your application need a transaction among the classes, you need to pass the connection through and set autocommit to false.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic