Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:08:16+00:00 2026-06-01T14:08:16+00:00

I am building DB class, in the constructor I want to establish the connection

  • 0

I am building DB class, in the constructor I want to establish the connection with database, so that static dbLink is accessible by the rest of the functions inside that class. Is that a good approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:08:17+00:00Added an answer on June 1, 2026 at 2:08 pm

    As to the concrete question, surely it’s legal to throw exceptions in a constructor. There’s no other sane way to prevent the "DB class" instance from being used with a broken connection.

    As to the concrete functional requirement, you’ve another major problem. You should not be creating a DB connection in the constructor of a "DB class" and surely not make it static. This indicates that you’re intending to keep the connection open as long as the instance of the "DB class" lives in Java’s memory. This is in turn a very bad idea. The connection should instead be created in the very same try block as where you’re executing the SQL query/queries. The connection should also be closed in the finally block of that try block. This prevents resource leaking in long term which would otherwise cause your application to crash because the DB server times out the resource because it’s been open for too long, or runs out of resources because too many connections have been opened.

    See also:

    • How often should Connection, Statement and ResultSet be closed in JDBC?
    • JDBC MySql connection pooling practices to avoid exhausted connection pool
    • When my app loses connection, how should I recover it?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I'm building a UDF class called StaticLookupUDF that has to load some static
I am building a class that turns a flatfile into a virtual database, I
I'm building a class library that will have some public & private methods. I
I'm building a class that automatically loads a DB record, looks at each DB
I am building a class that handles NSURLConnection requests. To allow other classes to
I'm building an MVC3 app, trying to use IoC and constructor injection. My database
For a University assignment I'm building a class structure that includes, in part, several
I'm building a model class in mvc asp.net and I want to use a
Simply question of theory When building a class that is not going to be
I building a simple class that is supposed to mimic the functionality of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.