Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Connection Class

 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DBManager db= DBManager.openDBConnection(); returns a Connection and not a DBManager!
 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Since this thread is two years old, you might not get a reply, I am afraid.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic