Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / April 2004

Tip: Looking for answers? Try searching our database.

Connection with a database

Thread view: 
Simonne De Ryck - 02 Apr 2004 18:39 GMT
Hello,

I' m writing a application that works with a database.  Every time that I
need information from the database I have to open an connection, and close
it back at the end.

Now I want to know if their is an way to open the connection when the
application the user starts the applicatin and to close this if the
applications will be terminated.

Thanks
Christophe Vanfleteren - 02 Apr 2004 18:45 GMT
> Hello,
>
[quoted text clipped - 7 lines]
>
> Thanks

Yes, just open a connection on startup, and keep using that same connection.

How to adjust your program to make this work, you'll have to figure out for
yourself. One hint though, a Singleton holding your connection would
probably be a good way to solve this.

Signature

Kind regards,
Christophe Vanfleteren

Simonne De Ryck - 02 Apr 2004 19:19 GMT
> > Hello,
> >
[quoted text clipped - 13 lines]
> yourself. One hint though, a Singleton holding your connection would
> probably be a good way to solve this.

Can you give me a little bit more info about how i do this exactly.
Or even better can I find somewhere an example of it.

thanks in advance.
Els Wauters
Christophe Vanfleteren - 02 Apr 2004 19:27 GMT
>> > Hello,
>> >
[quoted text clipped - 23 lines]
> thanks in advance.
> Els Wauters

You could use something like this:

public class ConnectionHolder {
       
       private static Connection con;

       public static Connection getConnection() {
               if(con == null) {
                       con =   //create the connection as you would otherwise
               }
               return con;
       }
}

Now, everytime you need the connection, you do like this:

Connection con = ConnectionHolder.getConnection();
//work with the connection

This will break once you start to use your connection from within several
threads though. In that case, you'd have to setup a Datasource or a
ConnectionPool.

Signature

Kind regards,
Christophe Vanfleteren

Josef Garvi - 05 Apr 2004 07:31 GMT
> You could use something like this:
>
[quoted text clipped - 14 lines]
> Connection con = ConnectionHolder.getConnection();
> //work with the connection

Do you also need to close() the connection before terminating the app?

Signature

Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty

Christophe Vanfleteren - 05 Apr 2004 11:11 GMT
<snip code>

> Do you also need to close() the connection before terminating the app?

Yes, that would be the best thing to do.
You always need to close() the resources you use when using JDBC, so always
close your ResultSets, Statements and Connections.

Signature

Kind regards,
Christophe Vanfleteren

Simonne De Ryck - 05 Apr 2004 20:34 GMT
> You could use something like this:
>
[quoted text clipped - 14 lines]
> Connection con = ConnectionHolder.getConnection();
> //work with the connection

This application will be used on a network.  So if I understand this
correctly I didn't start correctly.

What you mentionned  about using an singleton I'm going to try one of these
days, when I have enough time.

Kind regards & thanks
Els Wauters
Josef Garvi - 22 Apr 2004 10:21 GMT
> This will break once you start to use your connection from within several
> threads though. In that case, you'd have to setup a Datasource or a
> ConnectionPool.

Any hints on how to do that?

Signature

Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.