在其他对象之间共享JDBC连接对象 [英] sharing a JDBC Connection object between other objects
问题描述
我创建了一个Database类,该类使用静态连接对象,以便在其自身的实例之间共同使用.我的问题是这种方法是否存在问题?
I have created a Database class which uses a static connection object in order to be used in common between instances of itself. my question is that is there any problem with this approach or not?
class Database {
private static Connection connection = null;
public Database() {
if(connection == null){
...
connection = DriverManager.getConnection(...);
...
}
}
}
推荐答案
如果您每秒要进行许多(数百个)查询,那么实现连接池是您的最佳选择.有关更多详细信息,请参见
If you are going to have many (hundreds) of queries per second then implementing a connection pool is the way to go. See the answer to this question for more details. However, if you a Java novice (we all were one day!) then I don't imagine you will be needing this requirement, and probably will struggle to implement it.
相反,如果需要的话,创建一个新连接,然后在完成后将其关闭的简单模式将是您前进的最佳方法.下面是您的 Database
类的修改版本,我认为这是前进的好方法.
Instead, the simple pattern of creating a new connection if required, and then closing it when finished will be the best way to go forward for you. Below is a modified version of your Database
class which I think is a good way to move forward.
class Database {
private Connection con = null;
private final String connectionString;
public Database(String connectionString) {
this.connectionString = connectionString;
}
public void connect() throws SQLException {
if (con != null // if the connection exists
&& !con.isClosed() // and has not been closed
&& con.isValid(0)) { // and appears to be functioning (with a test timeout of 0ms)
return; // skip connection creation
}
// create the connection
con = DriverManager.getConnection(connectionString);
}
public void testFunction() {
try {
connect();
// .. do some stuff with the connection ..
} catch (Exception e) {
// log or otherwise deal with the error
} finally {
try {
con.close();
} catch (Exception e) {
System.err.println("Failed to close connection: " + e.toString());
}
}
}
}
有关此解决方案的一些注意事项:
Some things to note about this solution:
- 效率不是很高-与使用现有连接相比,创建新连接总是要花费更多时间
- 如果不是线程安全的此类-如果您需要此要求,建议使用线程池.但是,如果您为每个线程创建此类的新实例,则它 将是线程安全的(因为无需担心静态连接!)
- 它确实可以完成工作-当然,对于简单的情况也是如此.我将该模型用于相对较低的数据库,该数据库每分钟建立/关闭约50-100个连接,并且不会增加明显的延迟
- 它非常坚固[em] -没有什么比打开和关闭每个查询连接更安全的了.您一定可以处理每个查询的连接失败,并且连接将始终关闭(除非已经关闭).
- It is not very efficient - creating a new connection always takes more time than using an existing one
- This class if not thread safe - if you need this requirement, I recommend using a thread pool. However, if you create a new instance of this class per thread then it will be thread safe (as there is not static connection to worry about!)
- It does do the job - certainly for simple cases. I use the model for a relatively low volume database which has approx 50-100 connections made/closed per minute and it does not add a noticeable lag
- It is very robust - nothing is safer than opening and closing a connection per query. You are guaranteed to be able to handle a connection failure per query, and the connection will always be closed (unless it already has been).
免责声明上面的解决方案并不是特别出色的解决方案.但是,我相信它的实现很简单,并且是Java新手在进入外部库之前先了解绳索的好方法.
Disclaimer The solution above is not a particularly amazing solution. However, I believe it is simple to implement and a good way for a Java novice to get to know the ropes before jumping into external libraries.
这篇关于在其他对象之间共享JDBC连接对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!