Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am reading hibernate and I came across statements like "JDBC connections open across multiple requests is not a good thing because it is an expensive resource ".

What is meant by expensive resource ?

Edit : I am adding what user unknown has added as a comment below .

If establishing a database connection is expensive , then why not use the same thing again and again (not caching) , instead of closing it and opening it everytime ?

share|improve this question
Expensive means that it will hurt you if you use it too frequently or not in the manner specified. – Alexander Gessler Apr 4 '11 at 14:08
up vote 19 down vote accepted

An expensive resource is one that:

  • Takes a long time to create/initialize. Any database connection can take upwards of 1-2 seconds to complete the handshake.
  • Uses a lot of memory. Reading a complete file upload into a byte array in memory before storing it will use up a considerable amount of memory for a brief period of time.
  • Uses a lot of CPU cycles. Calculations on a large collection of data can tax your CPU while it is performing the calculations.

Each of the expensive resources may provide scalability problems. In short, you can't have too many of these expensive operations happening at the same time or you won't be able to respond to your users quickly enough.

share|improve this answer
I like to agree, but have to point out, that it conflicts with the example @Sun gave. Because setting up a connection is expensive, you would keep your connection open. – user unknown Apr 4 '11 at 13:13
In this particular case let me clarify: If something takes a long time to create/initialize you want to limit the number of times you create/initialize that resource. Database connections don't take a lot of resources while they are open, so it is preferable to pool these resources. – Berin Loritsch Apr 4 '11 at 13:18

It can be a variety of things dependent on the specific resource. So you might have the following:

  • How much memory it requires to operate.
  • The time taken to make the connection.
  • The overhead in maintaining that connection.

and so on.

Other things that can make something expensive could be if it requires access to the hard drive or the network during is operation etc.

share|improve this answer

Expensive should be prefixed with "computationally". Computational expense is used to refer to the amount of time or resources (CPU, memory, disk, network bandwith) required to complete an operation, algorithm or program.

share|improve this answer

The expense of database connections comes from a number of factors.

  • Database connections are slow to setup and require resources on the client and server sides. This is why connections are pooled.
  • Open transactions have an overhead which requires management by both client and server.
  • Open transactions may block other updates. This will slow down overall performance.

That said, you need keep the connection (or at least the transaction) open long enough to make sure your transaction passes the ACID test. (Atomic Consistent Isolated Durable)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.