Fixing minor issues in closing the database statements and resultsets

4.x.x
megala21 7 years ago
parent 149719d64c
commit f16b03ffee

@ -91,4 +91,4 @@ public class ErrorResponse {
public void setErrorItems(List<ErrorListItem> error) {
this.errorItems = error;
}
}
}

@ -66,13 +66,16 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
String insertPlatformProps =
"INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL, "
+ "DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)";
for (Platform.Property property : platform.getProperties()) {
preparedStatement = connection.prepareStatement(insertPlatformProps);
preparedStatement.setInt(1, platformId);
preparedStatement.setString(2, property.getName());
preparedStatement.setBoolean(3, property.isOptional());
preparedStatement.setString(4, property.getDefaultValue());
preparedStatement.execute();
if (platform.getProperties() != null) {
for (Platform.Property property : platform.getProperties()) {
preparedStatement = connection.prepareStatement(insertPlatformProps);
preparedStatement.setInt(1, platformId);
preparedStatement.setString(2, property.getName());
preparedStatement.setBoolean(3, property.isOptional());
preparedStatement.setString(4, property.getDefaultValue());
preparedStatement.execute();
}
}
} else {
String insertToPlatform =
@ -105,12 +108,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
throw ex;
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - "
+ platform.getIdentifier() + " for tenant - " + tenantId, e);
throw new PlatformManagementDAOException(
"Unable to obtain the connection while trying to register the platform - " + platform
.getIdentifier() + " for tenant - " + tenantId, e);
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error occurred while performing the transaction on the database " +
"for adding the platform - " + platform.getIdentifier() + " , tenant domain - " + tenantId);
throw new PlatformManagementDAOException(
"Error occurred while performing the transaction on the database " + "for adding the platform - "
+ platform.getIdentifier() + " , tenant domain - " + tenantId);
} finally {
ConnectionManagerUtil.closeConnection();
}
@ -119,6 +124,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
@Override
public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws
PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
try {
ConnectionManagerUtil.beginTransaction();
int platformId = getPlatformId(tenantId, oldPlatformIdentifier);
@ -127,7 +133,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
if (!platform.isFileBased()) {
String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ?, NAME =?, DESCRIPTION=?, "
+ "IS_SHARED=?, ICON_NAME=? WHERE ID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement.setString(1, platform.getIdentifier());
preparedStatement.setString(2, platform.getName());
preparedStatement.setString(3, platform.getDescription());
@ -154,7 +160,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
}
} else {
String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ? WHERE ID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement.setInt(1, platformId);
preparedStatement.execute();
}
@ -181,20 +187,23 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
"Error occurred while performing the transaction on the database " + "for adding the platform - "
+ platform.getIdentifier() + " , tenant domain - " + tenantId);
} finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
}
}
private int getPlatformId(int tenantId, String platformIdentifier) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND "
+ "IDENTIFIER=?)";
try {
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, tenantId);
preparedStatement.setString(2, platformIdentifier);
preparedStatement.setString(3, platformIdentifier);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt("ID");
}
@ -203,19 +212,22 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
throw new PlatformManagementDAOException("Error when trying to obtaining the database connection.", e);
} catch (SQLException e) {
throw new PlatformManagementDAOException("Error in executing the query - " + query, e);
} finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, resultSet);
}
}
@Override
public void unregister(int tenantId, String platformIdenfier) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
try {
ConnectionManagerUtil.beginTransaction();
int platformId = getPlatformId(tenantId, platformIdenfier);
if (platformId != -1) {
Connection connection = ConnectionManagerUtil.getConnection();
String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(deletePlatform);
preparedStatement = connection.prepareStatement(deletePlatform);
preparedStatement.setInt(1, platformId);
preparedStatement.execute();
ConnectionManagerUtil.commitTransaction();
@ -240,25 +252,27 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
}
}
public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementDAOException {
String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_ID, PLATFORM_ID) VALUES (?, ?)";
PreparedStatement preparedStatement = null;
try {
ConnectionManagerUtil.beginTransaction();
for (String platformIdentifier : platformIdentifiers) {
if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) {
int platformId = getPlatformId(tenantId, platformIdentifier);
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(insertMapping);
preparedStatement = connection.prepareStatement(insertMapping);
preparedStatement.setInt(1, tenantId);
preparedStatement.setInt(2, platformId);
preparedStatement.execute();
} else {
throw new PlatformManagementDAOException("Platform identifier - " + platformIdentifier + " is "
+ "already assigned to tenant domain - " + tenantId);
log.error("Platform identifier - " + platformIdentifier + " is already assigned to tenant domain"
+ " - " + tenantId);
}
}
ConnectionManagerUtil.commitTransaction();
@ -279,6 +293,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
}
}
@ -310,12 +325,13 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
@Override
public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException {
String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?";
PreparedStatement preparedStatement = null;
try {
ConnectionManagerUtil.beginTransaction();
int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier);
if (mappingId != -1) {
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(deleteMapping);
preparedStatement = connection.prepareStatement(deleteMapping);
preparedStatement.setInt(1, mappingId);
preparedStatement.execute();
ConnectionManagerUtil.commitTransaction();
@ -335,28 +351,31 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
}
}
@Override
public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier);
String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?";
try {
ConnectionManagerUtil.openConnection();
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(getMapping);
preparedStatement = connection.prepareStatement(getMapping);
preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID);
preparedStatement.setInt(2, platformId);
preparedStatement.execute();
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException(
"Error occured while obtaining the connection to get the existing " + "Tenant - Platform Mapping.",
"Error occurred while obtaining the connection to get the existing " + "Tenant - Platform Mapping.",
e);
} catch (SQLException e) {
throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e);
throw new PlatformManagementDAOException("Error occurred while executing the SQL query - " + getMapping, e);
} finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
}
}
@ -364,6 +383,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
@Override
public List<Platform> getPlatforms(int tenantId) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
if (log.isDebugEnabled()) {
log.debug("GetPlaforms request received for the tenant ID " + tenantId);
}
@ -373,9 +395,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
+ "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID";
try {
Connection connection = ConnectionManagerUtil.openConnection();
PreparedStatement preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setInt(1, tenantId);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet = preparedStatement.executeQuery();
List<Platform> platforms = new ArrayList<>();
if (log.isDebugEnabled()) {
log.debug("Platform retrieved for the tenant Id " + tenantId);
@ -401,30 +423,33 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
return platforms;
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException(
"Error occured when loading the platforms for tenant - " + tenantId, e);
"Error occurred when loading the platforms for tenant - " + tenantId, e);
} catch (SQLException e) {
throw new PlatformManagementDAOException("Error occurred when executing query - " + selectQuery, e);
} finally {
ConnectionManagerUtil.cleanupResources(preparedStatement,resultSet);
ConnectionManagerUtil.closeConnection();
}
}
public Platform getPlatform(String tenantDomain, String platformIdenfier) throws PlatformManagementDAOException {
public Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) " +
"OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " +
"LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID";
try {
ConnectionManagerUtil.openConnection();
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(platformQuery);
preparedStatement = connection.prepareStatement(platformQuery);
preparedStatement.setString(1, tenantDomain);
preparedStatement.setString(2, platformIdenfier);
preparedStatement.setString(3, platformIdenfier);
ResultSet resultSet = preparedStatement.executeQuery();
preparedStatement.setString(2, platformIdentifier);
preparedStatement.setString(3, platformIdentifier);
resultSet = preparedStatement.executeQuery();
Platform platform = new Platform();
if (resultSet.next()) {
platform.setId(resultSet.getInt("PLATFORM.ID"));
platform.setIdentifier(platformIdenfier);
platform.setIdentifier(platformIdentifier);
platform.setName(resultSet.getString("PLATFORM.NAME"));
platform.setIconName(resultSet.getString("PLATFORM.DESCRIPTION"));
platform.setIconName(resultSet.getString("PLATFORM.ICON_NAME"));
@ -442,27 +467,29 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} while (resultSet.next());
platform.setProperties(properties);
} else {
platform.setIdentifier(platformIdenfier);
platform.setIdentifier(platformIdentifier);
platform.setFileBased(true);
}
return platform;
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException("Error when loading the platform - " + platformIdenfier, e);
throw new PlatformManagementDAOException("Error when loading the platform - " + platformIdentifier, e);
} catch (SQLException e) {
throw new PlatformManagementDAOException("Error in executing the query - " + platformQuery, e);
} finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, resultSet);
ConnectionManagerUtil.closeConnection();
}
}
public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = "";
try {
conn = this.getConnection();
sql += "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?";
sql = "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, identifier);
@ -484,14 +511,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
platform.setShared(rs.getBoolean("IS_SHARED"));
}
}
return platform;
} catch (SQLException e) {
throw new PlatformManagementDAOException("Error occurred while getting application List", e);
throw new PlatformManagementDAOException("Error occurred while getting platform with the identifier " +
identifier + ", for the tenant : " + tenantId, e);
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException("Error occurred while obtaining the DB connection.", e);
} finally {
ConnectionManagerUtil.cleanupResources(stmt, rs);
}
}
}

@ -37,6 +37,10 @@ import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
/**
* PlatformDeployer is responsible for deploying platforms that are added in the filesystem.
* This will deploy the platforms that are added in <IOT_HOME>/repository/deployment/server/platforms directory.
*/
public class PlatformDeployer extends AbstractDeployer {
private static final Log log = LogFactory.getLog(PlatformDeployer.class);
@ -53,6 +57,7 @@ public class PlatformDeployer extends AbstractDeployer {
}
}
@Override
public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException {
File deploymentFile = new File(deploymentFileData.getAbsolutePath());
try {
@ -63,6 +68,7 @@ public class PlatformDeployer extends AbstractDeployer {
org.wso2.carbon.device.application.mgt.common.Platform platform = convert(platformConf);
DataHolder.getInstance().getPlatformManager()
.register(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platform);
log.info("Platform configuration : " + deploymentFile.getName() + " deployed successfully");
} else {
log.error("Unable to deploy the platform - " + deploymentFile.getAbsolutePath()
+ "!. Platform config file name - " + deploymentFile.getName()
@ -75,13 +81,15 @@ public class PlatformDeployer extends AbstractDeployer {
}
}
@Override
public void undeploy(String fileName) throws DeploymentException {
String platformId = getPlatformID(fileName);
try {
DataHolder.getInstance().getPlatformManager()
.unregister(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platformId, true);
log.info("Platform configuration : " + fileName + " un-deployed successfully");
} catch (PlatformManagementException e) {
log.error("Error occurred while undeploying the platform - " + fileName);
log.error("Error occurred while un-deploying the platform - " + fileName);
}
}

@ -226,9 +226,8 @@ public class PlatformManagerImpl implements PlatformManager {
if (tenantPlatforms != null) {
this.inMemoryStore.remove(identifier);
}
} else {
DAOFactory.getPlatformDAO().unregister(tenantId, identifier);
}
DAOFactory.getPlatformDAO().unregister(tenantId, identifier);
}
@Override

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
@ -18,14 +18,13 @@
package org.wso2.carbon.device.application.mgt.core.util;
import org.apache.commons.logging.Log;
import java.io.File;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.dbcreator.DatabaseCreator;
import javax.sql.DataSource;
import java.io.File;
/**
* ApplicationMgtDatabaseCreator is responsible for creating the Application Management related tables.
*/

@ -27,6 +27,8 @@ import org.wso2.carbon.device.application.mgt.common.exception.TransactionManage
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ConnectionManagerUtil {
@ -185,4 +187,27 @@ public class ConnectionManagerUtil {
return null;
}
/**
* Cleanup resources used to transaction
*
* @param stmt Prepared statement used
* @param rs Obtained results set
*/
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Error occurred while closing result set", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing prepared statement", e);
}
}
}
}

Loading…
Cancel
Save