Hi Friends,
Please send me your openion regarding this issue.
Actually i am using a DAOImpl class where i will get the data base connection. The code snippet for one method is like this...
And i am repeating the same procedure in each method like opening the connection and closing the connection in each method.
My problem is:
Is it right to open and closing connection in each method of DAO implementaton class?
and how can i alter the code if there exist any performance issue?
This application is a web based appliation using
struts.
In the Action class i am calling each required implementation method.
public EmployeeDAOImpl(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
Checks if an employee is Admin for the specified module in RMS_MODULE_ADMINS table.
**/
public boolean isAdmin(
String employeeId, String moduleId) {
logger
.debug("Entered - isAdmin() of EmployeeDAOIMPL with arguments EmployeeId:"
+ employeeId + "and ModuleId:" + moduleId + ".");
Connection conn_Admin = null;
PreparedStatement pst_Admin = null;
ResultSet rs_Admin = null;
boolean isAdmin = false;
try {
conn_Admin = dataSource.getConnection();
pst_Admin = conn_Admin
.prepareStatement("SELECT ROLE_ID FROM RMS_MODULE_ADMINS "
+ "WHERE EMP_ID = ? AND MODULE_ID = ?");
pst_Admin.setString(1, employeeId.toLowerCase());
pst_Admin.setString(2, moduleId);
rs_Admin = pst_Admin.executeQuery();
if (rs_Admin.next()) {
isAdmin = true;
}
} catch (Exception e) {
logger.error("Error in isAdmin() of EmployeeDAOIMPL:"
+ e.getMessage());
} finally {
Util.closeConnection(rs_Admin, pst_Admin, conn_Admin);
logger.debug("Exiting - isAdmin() of EmployeeDAOIMPL");
}
return isAdmin;
}