• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access a Singleton for get connection to database ?  RSS feed

 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm developing a Singleton class for to do connection with database, only. After, a class to access the object for get connection and to do the query to database. However, I cannot get the connection for the object. See my class (Singleton):



My class get the connection and do the query.

 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gustavo Siqueira wrote:I cannot get the connection for the object.


How do you know this? Do you have error messages? Program doesn't do what you thought? Read our FAQ entry TellTheDetails to learn how to ask a better question.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to get the connection through of the ConnectDB class.
What you not understood ?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul asked you what makes you think that the program doesn't work.

On a quick look why you assign the database connection to an instance, and return an another unassigned instance.

Why singleton instance to hold connection object? Aren't you looking to close the connection at some point?
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See



Error the to create statement.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gustavo Siqueira wrote:Error the to create statement.


Can you tell us about this error? Just "Error" is not very much information. I already referred you to our TellTheDetails document. That TellTheDetails is a link. Click on it to follow it.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Paul!

The tomcat not give me no info reference to error.
I get only this:

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is line 30 of "MYCLASS.java"?
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you get your code working, you should seriously move away from using the Singleton pattern. Singletons are useful on occasion, but are probably the most overused construct in object-oriented programming. They solve very few real world problems and cause many.

Don't use singletons. Singletons considered harmful.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, what would the ways correct for do this ?
Using Connection Pool ?
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gustavo Siqueira wrote:Then, what would the ways correct for do this ?
Using Connection Pool ?


What do you mean by "this"? There is no reason to use a Singleton if all you want to do is to allocate one connection. You can simply use a Factory.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I will to try clarify the my problem.
I'm developing a site that contains multiples connections with database. I thought of the following way: An class only that be will responsible by the connections. I will to use this class for to call the connections for I to do query, without to have that create several class for this. Only a class for connections get.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why? It looks like you're using Tomcat. You can configure a connection pool for database connections in Tomcat, you don't have to write one yourself.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was solved, using that Singleton:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!