• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Singleton Connection Class

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75842
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic