开发者

Java server and PostgreSQL connection

开发者 https://www.devze.com 2023-04-12 17:02 出处:网络
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 Postg开发者_如何学GoreSQL server

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 Postg开发者_如何学GoreSQL 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


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消