以下是我目前在一个抽象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的信息,因为这是重要的内容。