简体   繁体   English

jdbc connection with java, singleton connection object or spring jdbc connection?

[英]jdbc connection with java, singleton connection object or spring jdbc connection?

I am manipulating a postgresql database with java.我正在使用 java 操作 postgresql 数据库。 Please help me to choose a development practice.请帮我选择一个开发实践。

Must I:我必须吗:

  • create a jdbc singleton object?创建一个 jdbc singleton object?
  • create a new connection for every request?为每个请求创建一个新连接?
  • or use the jdbc api for spring framework?或者将 jdbc api 用于 spring 框架?

Which is the best practice?哪个是最佳实践?

A vague answer for a vague question:模糊问题的模糊答案:

Rather have your JDBC connections managed.而是管理您的 JDBC 连接。 If you have too many connections open, you will have to maintain them and make sure they're closed for other connections to access the database (you can have an Exception stating "Too many open files").如果您打开了太多连接,则必须维护它们并确保它们已关闭以供其他连接访问数据库(您可能会出现“打开的文件过多”的异常)。 Connection Pooling maintains your connection.连接池维护您的连接。 Have 1 connection per request and once you're done, return it back to the pool.每个请求有 1 个连接,完成后,将其返回到池中。

I would do this:我会这样做:

  • Have my JDBC connections maintained in a Connection Pool (thanks Jigar Joshi ).连接池中维护我的 JDBC 连接(感谢Jigar Joshi )。
  • Request the connection from the Connection Pool and use it in my DAO (the DAO is what does the CRUD of my object to DB).从连接池请求连接并在我的DAO中使用它(DAO 是我的 object 到 DB 的 CRUD)。
  • Once connection is done, return the connection to the Connection Pool.连接完成后,将连接返回到连接池。

If you're using Spring, use the Spring JDBC Template .如果您使用 Spring,请使用Spring JDBC 模板

Where can I find good instructions or a tutorial on how to do connection pooling for JDBC to a Postgres database on my client?我在哪里可以找到关于如何将 JDBC 连接到客户端上的 Postgres 数据库的良好说明或教程?

http://www.mchange.com/projects/c3p0/index.html http://www.mchange.com/projects/c3p0/index.html

c3p0 was designed to be butt-simple to use. c3p0 被设计为易于使用。 Just put the jar file [lib/c3p0-0.9.0.jar] in your application's effective CLASSPATH, and make a DataSource like this:只需将 jar 文件 [lib/c3p0-0.9.0.jar] 放入应用程序的有效 CLASSPATH 中,然后制作如下所示的 DataSource:

import com.mchange.v2.c3p0.*;

...

ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver            
cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
cpds.setUser("dbuser");                                  
cpds.setPassword("dbpassword");    

[Optional] If you want to turn on PreparedStatement pooling, you must also set maxStatements and/or maxStatementsPerConnection (both default to 0): [可选] 如果要开启PreparedStatement池,还必须设置maxStatements和/或maxStatementsPerConnection (均默认为 0):

cpds.setMaxStatements( 180 ); 

Do whatever you want with your DataSource, which will be backed by a Connection pool set up with default parameters.用你的数据源做任何你想做的事,这将由一个使用默认参数设置的连接池支持。 You can bind the DataSource to a JNDI name service, or use it directly, as you prefer.您可以将 DataSource 绑定到 JNDI 名称服务,或者根据您的喜好直接使用它。 When you are done, you can clean up the DataSource you've created like this:完成后,您可以像这样清理已创建的 DataSource:

DataSources.destroy( cpds );

That's it.而已。 The rest is detail. rest 是细节。

Maintain a single connection or even better use Connection-pooling保持单个连接甚至更好地使用连接池

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM