Refractor DAO Fatcory connections

This commit is contained in:
manoj 2015-04-20 18:34:42 +05:30
parent df99f5a245
commit eec444201d

View File

@ -128,10 +128,17 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa
}
}
public static Connection getConnection() {
public static Connection getConnection() throws MobileDeviceManagementDAOException {
if (currentConnection.get() == null) {
try {
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection",
e);
}
}
return currentConnection.get();
}
public static void commitTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
@ -145,9 +152,22 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
}
}
public static void closeConnection() throws MobileDeviceManagementDAOException {
Connection con = currentConnection.get();
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
}
currentConnection.remove();
}
public static void rollbackTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
@ -161,6 +181,8 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while rollbacking the transaction", e);
} finally {
closeConnection();
}
}