Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

SQL Connections - Persistent or as needed?

connection

  • Please log in to reply
5 replies to this topic

#1 Timinator

Timinator

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 15 January 2011 - 09:29 AM

I'm currently developing my first Java desktop application using MSSQL as a back end. I've initially designed the app to have a database object which would allow me to pass my sql statements to the object and then have the object send the resultset back. However, it seems that the calling object has to carry the overhead of the try/catch code just to call the database object.

So, my question is, should I just be opening and using my database only when needed and code for each occurrence where it is needed? Or am I heading down the right path of opening my database at the start of app load and pass sql/resultsets to my database object?

Thx.
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 15 January 2011 - 05:56 PM

Certain databases have limited number of maximum connections, in a web environment a persistent connection is very limiting. In your case of a desktop application, and MSSQL being slow at connections as needed a persistant connection can be advantageous if you are using it in a constant manner.

If you are just utilizing a database on-load and on-save (occasionally) then you can consider if you would be advantageous or not.
  • 1

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 16 January 2011 - 06:19 AM

I do almost all of my database work in Delphi, not Java. With that said, for database heavy applications it's usually worthwhile to have a single, persistent database connection object, and queries that use it as needed. Error trapping is generally done in the code that has the query, since that is where you can actually interpret the significance and nature of the error, and react accordingly.

SQL queries can throw a lot of errors that range from "you completely hosed up this statement, programmer", to "yeah, it should have worked, but somebody else beat you to this primary key".
  • 1

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 Timinator

Timinator

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 18 January 2011 - 03:11 PM

Delphi is pretty cool. I'm an old Turbo Pascal hack from way back. However, I'm doing this to make myself more marketable. Looks like I'll go with the persistent connection. Plus, I like being able to write all my error traps in one spot.

Thanks for the feedback.
  • 0

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 18 January 2011 - 04:39 PM

I usually put my error traps around where I'm handling individual queries (either making the query or processing the record set). It's easier to control detailed error messages that way :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 sam_l

sam_l

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 52 posts

Posted 19 January 2011 - 03:03 AM

Due to the large resource cost associated with database connections I would use the singleton pattern, especially if you only need a single connection to a single database. This should minimize resource usage and allow for easy use.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download