【发布时间】:2011-03-31 05:06:52
【问题描述】:
以下是我目前在 Abstract DAO 类中使用的方法。如果有并发调用,它们是安全的还是应该使用同步?我知道如果对方法范围之外的属性的引用应该使用同步,但我不清楚应该如何使用外部资源处理事情。
public Connection getConnection() {
// Call to singleton handling JDBC stuff
return Database.getInstance().getCon();
}
public boolean isConnectionAvailable(){
if( getConnection() != null ){
return true;
}
return false;
}
public PreparedStatement getPreparedStatement( String sqlStatement ){
Connection connection = getConnection();
PreparedStatement pS = null;
if( connection != null ){
try {
pS = connection.prepareStatement( sqlStatement );
} catch (SQLException e) {
return null;
}
}
return pS;
}
编辑:我可能会重新表述这个问题,以包含有关编写 DAO 的信息,因为这在这里很重要。
【问题讨论】:
-
不要不要不要不要使用单例进行数据库连接访问
-
我经常听到这个。但是,当您使用纯 Java 时,还有什么选择?
标签: java jdbc synchronization dao