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 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.
- Create db
- Add hibernate to your project (project properties/frameworks)
- Create classes for the objects you want to save (take a look at "annotations"
- Create a sessionmanager or just google for it.
- 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
精彩评论