0

create server made in Java. The server accept request on port 111, and when a client connect to the server send to it a JSON file. The server receive the JSON then he must store into a postgres database.

My question is how to manage the postgres connection.

Should I create only one Connection and synchronize it for every client request or create a new connection for every client connected to the server.

I mean:

--------- Only one connection: --------------

The server create the only one connection with

_connection = DriverManager.
   getConnection("jdbc:postgresql:"+_dbName, _username, _password);

and for every clients use this connection

syncronize(_connection) { send data to database }

--------- A connection for every client -------------

The server when accept a client request create a new connection for the connected client

_clientConnection = DriverManager.
   getConnection("jdbc:postgresql:"+_dbName, _username, _password);

And every client has a personal connection.

Can anyone explain what is the best working practice for do this stuff? I think is the same with MySQL.

1 Answer 1

4

The best practice is to use a pool of connections. This makes sure that

  • multiple clients can access the database concurrently
  • the number of open connections always stays reasonable, and doesn't put the server or database to its knees in case of a high number of concurrent requests
  • connections are not constantly opened and closed. Opening a connection is a time and memory consuming operation

There are several free connection pools available (C3P0, DBCP, etc.). Google is your friend.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I founded this code on google: postgresql.org/docs/7.4/static/jdbc-datasource.html My question is, using Jdbc3PoolingDataSource I need to setup anything on my postgres linux server (like edit some config files on my linux machine) ??
I don't see why you should. Just make sure that the number of available connections on the postgres server matches with the max number of connections of the pool. But for the db server, a pooled connection is not different from a non-pooled connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.