0

I would like to make a server (A) in Java. Some clients (B) connect to A and send informations. When (A) receives data from (B) it must store those information into a PostgreSQL server (C).

I want to know how can I manage the connection between (A) and (C). I have two ideas:

1) Create an initial connection from (A) to (B). This connection remains always active. 2) Create a connection between (A) and (B) every time (A) receive a connection from (B), then close the connection (open/close a connection to the db from every client connect to the server)

My worry is about the database timeout.

Thank you

BQuadra
  • 769
  • 2
  • 10
  • 20

3 Answers3

3

Use a JDBC connection pool. It will reuse connections when it can, and create new connections as needed.

Then get a connection from the connection pool every time you need to do anything.

Community
  • 1
  • 1
Brendan Long
  • 51,248
  • 19
  • 139
  • 179
3

Use a JDBC connection pool. Better yet, break out your JEE/Spring hat and don't reinvent the wheel.

Stephan
  • 40,082
  • 60
  • 228
  • 319
Perception
  • 77,470
  • 19
  • 176
  • 187
  • These days, it's just JEE or Java EE. It hasn't been called J2EE since 2004. The name was changed with the release of Java 5. – Matt Ball Oct 13 '11 at 23:38
0

You really should take a look at the hibernate framwork. Not that difficult to implement in your webapplication and its just a solid way to do it.

  1. Create db
  2. Add hibernate to your project (project properties/frameworks)
  3. Create classes for the objects you want to save (take a look at "annotations"
  4. Create a sessionmanager or just google for it.
  5. Make a DAO (data access object) for eacht class

You can now add al the methods you want to DAO for saving and getting data from database

Then open a session in your controller and let the DAOs do all the work for you

jperez
  • 970
  • 1
  • 9
  • 25