| Author |
Singleton Connection Class
|
Faiz Abdelhafid
Ranch Hand
Joined: Nov 21, 2009
Posts: 32
|
|
Hello,
I am trying to access a singleton connection class from a test class but I got an incompatible type error.
here is the connection class:
and the test class
The error that I get is : incompatible type required : DBManager found: java.sql.connection.
Thanks !
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
You need a Connection object. The openDBConnection method returns a Connection object. So... what type of variable do you think you should assign the result of the openDBConnection method to?
|
 |
Faiz Abdelhafid
Ranch Hand
Joined: Nov 21, 2009
Posts: 32
|
|
Hello,
Well with the Singleton pattern I should use an object of the same type for the connection class, isn't it?
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
I don't follow. You have a method that returns the type java.sql.Connection and you are trying to assign it to a variable that expects the type DBManager. Perhaps the concept of a singleton is confusing the issue? Why do you want to use a singleton? What you have coded is not a singleton, by the way.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
DBManager db= DBManager.openDBConnection(); returns a Connection and not a DBManager!
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
Faiz Abdelhafid
Ranch Hand
Joined: Nov 21, 2009
Posts: 32
|
|
quote=Jothi Shankar Kumar]DBManager db= DBManager.openDBConnection(); returns a Connection and not a DBManager!
Hello,
Yes in fact the class return a connection which from Statement is inherited. [
|
 |
Faiz Abdelhafid
Ranch Hand
Joined: Nov 21, 2009
Posts: 32
|
|
Paul Sturrock wrote:I don't follow. You have a method that returns the type java.sql.Connection and you are trying to assign it to a variable that expects the type DBManager. Perhaps the concept of a singleton is confusing the issue? Why do you want to use a singleton? What you have coded is not a singleton, by the way.
Hello Paul,
Well the main idea behind coding the class as a singleton pattern is because I need just a one connection object a long with the system, and also disabling creating another instance from the connection.
I solved the problem thanks !
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
Faiz Abdelhafid wrote:
Paul Sturrock wrote:I don't follow. You have a method that returns the type java.sql.Connection and you are trying to assign it to a variable that expects the type DBManager. Perhaps the concept of a singleton is confusing the issue? Why do you want to use a singleton? What you have coded is not a singleton, by the way.
Hello Paul,
Well the main idea behind coding the class as a singleton pattern is because I need just a one connection object a long with the system, and also disabling creating another instance from the connection.
I solved the problem thanks ! 
OK, but what you've got above is not a singleton, many instances of DBManager can exist using that code.
|
 |
Faiz Abdelhafid
Ranch Hand
Joined: Nov 21, 2009
Posts: 32
|
|
Paul Sturrock wrote:
Faiz Abdelhafid wrote:
Paul Sturrock wrote:I don't follow. You have a method that returns the type java.sql.Connection and you are trying to assign it to a variable that expects the type DBManager. Perhaps the concept of a singleton is confusing the issue? Why do you want to use a singleton? What you have coded is not a singleton, by the way.
Hello Paul,
Well the main idea behind coding the class as a singleton pattern is because I need just a one connection object a long with the system, and also disabling creating another instance from the connection.
I solved the problem thanks ! 
OK, but what you've got above is not a singleton, many instances of DBManager can exist using that code.
The DBManager class uses a private constructor a side with a public static method to "share" the connection but not creating a new "instance".
I will ask my trainer for further details about the connection class.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
I can easily clone your class to create a new instance. I don't understand your argument. the openConnection() method returns a Connection and not a DBManager. Are you disagreeing with that?
|
 |
Faiz Abdelhafid
Ranch Hand
Joined: Nov 21, 2009
Posts: 32
|
|
Jothi Shankar Kumar wrote:I can easily clone your class to create a new instance. I don't understand your argument. the openConnection() method returns a Connection and not a DBManager. Are you disagreeing with that?
Hello Jothi,
The whole idea was to create a single shared instance for the application inside the static method:
which will share this object among all users with the same DBConnection object
And then you gotta only write the following in your application
Hope I clarified my point of view
BR
|
 |
hira sree
Greenhorn
Joined: Mar 23, 2012
Posts: 1
|
|
Is it possible for you to share the complete code...i am trying very hard from last couple of days to figure this out.
I want to have a singleton database java code loading from the property file
Thanks a bunch
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Welcome to the Ranch
Since this thread is two years old, you might not get a reply, I am afraid.
|
 |
 |
|
|
subject: Singleton Connection Class
|
|
|