简体   繁体   English

很少的JDBC连接

[英]Few JDBC connections

As I know connection is kind of very expensive operation. 据我所知,连接是一种非常昂贵的操作。 Which approach should I follow to handle few connections? 我应该遵循哪种方法处理少量连接? I have seen a lots of examples where author talks about only one connection or uses connection pool(from JBoss or something). 我看到了很多例子,其中作者仅谈论一个连接或使用连接池(来自JBoss或其他东西)。 My code looks like this: 我的代码如下所示:

1) make new connection 1)建立新的连接

2) do some logic 2)做一些逻辑

3) close connection 3)紧密连接

DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());    
con = DriverManager.getConnection(URL, username, password);  
if(con!=null) 
   System.out.println("Connection Successful !\n");  
//logic      
if(con!=null)
   con.close();

But I've got few similar methods based on this approach. 但是基于这种方法,我几乎没有类似的方法。 So do I really have to make new connection or should I use the only one? 那么,我是否真的必须进行新连接还是应该仅使用一个连接? If I use one connection, do I need to close it after? 如果我使用一个连接,之后是否需要关闭它?

PS. PS。 App is not a servlet. 应用不是servlet。

You can use a connection pool like http://sourceforge.net/projects/c3p0/ or http://jolbox.com/ to abstract from this problem. 您可以使用http://sourceforge.net/projects/c3p0/http://jolbox.com/之类的连接池来抽象此问题。

If you do not close connections after use, you will have lots of opening connections that are not in use (until database cleans up). 如果在使用后不关闭连接,则将有许多未使用的打开连接(直到数据库清理完毕)。

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

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