refactored DAO handler

revert-70aa11f8
ayyoob 8 years ago
parent d17241e51e
commit fbfb86c53e

@ -0,0 +1,107 @@
package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeDeployerFileException;
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* This component handles the connections
*/
public class DeviceTypeDAOHandler {
private static final Log log = LogFactory.getLog(DeviceTypeDAOHandler.class);
private DataSource dataSource;
private ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public DeviceTypeDAOHandler(String datasourceName) {
initDAO(datasourceName);
}
public void initDAO(String datasourceName) {
try {
Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup(datasourceName);
} catch (NamingException e) {
throw new DeviceTypeDeployerFileException("Error while looking up the data source: " + datasourceName, e);
}
}
public void beginTransaction() throws DeviceTypeMgtPluginException {
try {
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while retrieving datasource connection", e);
}
}
public Connection getConnection() throws DeviceTypeMgtPluginException {
if (currentConnection.get() == null) {
try {
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while retrieving data source connection", e);
}
}
return currentConnection.get();
}
public void commitTransaction() throws DeviceTypeMgtPluginException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit "
+ "has not been attempted");
}
}
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
}
}
public void closeConnection() throws DeviceTypeMgtPluginException {
Connection con = currentConnection.get();
if (con != null) {
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
}
}
currentConnection.remove();
}
public void rollbackTransaction() throws DeviceTypeMgtPluginException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback "
+ "has not been attempted");
}
}
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while rollback the transaction", e);
} finally {
closeConnection();
}
}
}

@ -39,12 +39,12 @@ import java.util.List;
public class DeviceTypePluginDAO {
private static final Log log = LogFactory.getLog(DeviceTypePluginDAO.class);
private DeviceTypePluginDAOManager deviceTypePluginDAOManager;
private DeviceTypeDAOHandler deviceTypeDAOHandler;
private DeviceDAODefinition deviceDAODefinition;
public DeviceTypePluginDAO(DeviceDAODefinition deviceDAODefinition,
DeviceTypePluginDAOManager deviceTypePluginDAOManager) {
this.deviceTypePluginDAOManager = deviceTypePluginDAOManager;
DeviceTypeDAOHandler deviceTypeDAOHandler) {
this.deviceTypeDAOHandler = deviceTypeDAOHandler;
this.deviceDAODefinition = deviceDAODefinition;
}
@ -54,7 +54,7 @@ public class DeviceTypePluginDAO {
Device device = null;
ResultSet resultSet = null;
try {
conn = deviceTypePluginDAOManager.getConnection();
conn = deviceTypeDAOHandler.getConnection();
String selectDBQuery = "SELECT " + getDeviceTableColumnNames() + " FROM " +
deviceDAODefinition.getDeviceTableName() + " WHERE " + deviceDAODefinition.getPrimarkey() + " = ?";
stmt = conn.prepareStatement(selectDBQuery);
@ -84,7 +84,7 @@ public class DeviceTypePluginDAO {
throw new DeviceTypeMgtPluginException(msg, e);
} finally {
DeviceTypeUtils.cleanupResources(stmt, resultSet);
deviceTypePluginDAOManager.closeConnection();
deviceTypeDAOHandler.closeConnection();
}
return device;
@ -95,7 +95,7 @@ public class DeviceTypePluginDAO {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = deviceTypePluginDAOManager.getConnection();
conn = deviceTypeDAOHandler.getConnection();
String createDBQuery = "INSERT INTO " + deviceDAODefinition.getDeviceTableName() + "("
+ deviceDAODefinition.getPrimarkey() + " , " + getDeviceTableColumnNames() + ") VALUES ("
+ getPreparedInputString(deviceDAODefinition.getColumnNames().size() + 1) + ")";
@ -130,7 +130,7 @@ public class DeviceTypePluginDAO {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = deviceTypePluginDAOManager.getConnection();
conn = deviceTypeDAOHandler.getConnection();
String updateDBQuery = "UPDATE " + deviceDAODefinition.getDeviceTableName() + " SET "
+ getDeviceTableColumnNamesForUpdateQuery()+ " WHERE " + deviceDAODefinition.getPrimarkey()
+ " = ?";
@ -165,7 +165,7 @@ public class DeviceTypePluginDAO {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = deviceTypePluginDAOManager.getConnection();
conn = deviceTypeDAOHandler.getConnection();
String deleteDBQuery = "DELETE FROM " + deviceDAODefinition.getDeviceTableName()
+ " WHERE " + deviceDAODefinition.getPrimarkey() + " = ?";
stmt = conn.prepareStatement(deleteDBQuery);
@ -196,7 +196,7 @@ public class DeviceTypePluginDAO {
Device device;
List<Device> devices = new ArrayList<>();
try {
conn = deviceTypePluginDAOManager.getConnection();
conn = deviceTypeDAOHandler.getConnection();
String selectDBQuery = "SELECT " + getDeviceTableColumnNames() + " FROM "
+ deviceDAODefinition.getDeviceTableName();
stmt = conn.prepareStatement(selectDBQuery);
@ -226,7 +226,7 @@ public class DeviceTypePluginDAO {
throw new DeviceTypeMgtPluginException(msg, e);
} finally {
DeviceTypeUtils.cleanupResources(stmt, resultSet);
deviceTypePluginDAOManager.closeConnection();
deviceTypeDAOHandler.closeConnection();
}
}

@ -19,111 +19,20 @@
package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceManagementConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeDeployerFileException;
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class DeviceTypePluginDAOManager {
private static final Log log = LogFactory.getLog(DeviceTypePluginDAOManager.class);
private DataSource dataSource;
private ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
private DeviceTypePluginDAO deviceTypePluginDAO;
public DeviceTypePluginDAOManager(String datasourceName, DeviceDAODefinition deviceDAODefinition) {
initDAO(datasourceName);
deviceTypePluginDAO = new DeviceTypePluginDAO(deviceDAODefinition, this);
DeviceTypeDAOHandler deviceTypeDAOHandler = new DeviceTypeDAOHandler(datasourceName);
deviceTypePluginDAO = new DeviceTypePluginDAO(deviceDAODefinition, deviceTypeDAOHandler);
}
public void initDAO(String datasourceName) {
try {
Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup(datasourceName);
} catch (NamingException e) {
throw new DeviceTypeDeployerFileException("Error while looking up the data source: " + datasourceName, e);
}
}
public DeviceTypePluginDAO getDeviceDAO() {
return deviceTypePluginDAO;
}
public void beginTransaction() throws DeviceTypeMgtPluginException {
try {
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while retrieving datasource connection", e);
}
}
public Connection getConnection() throws DeviceTypeMgtPluginException {
if (currentConnection.get() == null) {
try {
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while retrieving data source connection", e);
}
}
return currentConnection.get();
}
public void commitTransaction() throws DeviceTypeMgtPluginException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit "
+ "has not been attempted");
}
}
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
}
}
public void closeConnection() throws DeviceTypeMgtPluginException {
Connection con = currentConnection.get();
if (con != null) {
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
}
}
currentConnection.remove();
}
public void rollbackTransaction() throws DeviceTypeMgtPluginException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback "
+ "has not been attempted");
}
}
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while rollback the transaction", e);
} finally {
closeConnection();
}
}
}
Loading…
Cancel
Save