BQuadra
BQuadra

Reputation: 819

Java server and PostgreSQL connection

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

Upvotes: 0

Views: 818

Answers (3)

Perception
Perception

Reputation: 80633

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

Upvotes: 3

jperez
jperez

Reputation: 1060

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

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54312

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.

Upvotes: 3

Related Questions