Merge remote-tracking branch 'upstream/application-mgt' into appm_new

feature/appm-store/pbac
megala21 7 years ago
commit 2ab43c89ac

@ -25,6 +25,11 @@ import java.sql.Connection;
public abstract class AbstractDAOImpl { public abstract class AbstractDAOImpl {
protected Connection getDBConnection() throws DBConnectionException {
return ConnectionManagerUtil.getDBConnection();
}
@Deprecated
protected Connection getConnection() throws DBConnectionException { protected Connection getConnection() throws DBConnectionException {
return ConnectionManagerUtil.getConnection(); return ConnectionManagerUtil.getConnection();
} }

@ -195,7 +195,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
conn = this.getConnection(); conn = this.getDBConnection();
String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ?"; String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, uuid); stmt.setString(1, uuid);
@ -220,7 +220,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
int id = 0; int id = 0;
try { try {
conn = this.getConnection(); conn = this.getDBConnection();
sql += "SELECT ID FROM APPM_APPLICATION WHERE UUID = ?"; sql += "SELECT ID FROM APPM_APPLICATION WHERE UUID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, uuid); stmt.setString(1, uuid);
@ -360,7 +360,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
conn = this.getConnection(); conn = this.getDBConnection();
String sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?"; String sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationId); stmt.setInt(1, applicationId);
@ -382,7 +382,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
conn = this.getConnection(); conn = this.getDBConnection();
String sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?"; String sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationId); stmt.setInt(1, applicationId);

@ -116,24 +116,22 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public void deleteApplication(String uuid) throws ApplicationManagementException { public void deleteApplication(String uuid) throws ApplicationManagementException {
try { try {
ConnectionManagerUtil.openConnection();
ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
int appId = applicationDAO.getApplicationId(uuid); int appId = applicationDAO.getApplicationId(uuid);
ConnectionManagerUtil.beginTransaction(); ConnectionManagerUtil.beginDBTransaction();
applicationDAO.deleteTags(appId); applicationDAO.deleteTags(appId);
applicationDAO.deleteProperties(appId); applicationDAO.deleteProperties(appId);
applicationDAO.deleteApplication(uuid); applicationDAO.deleteApplication(uuid);
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Failed to delete application: " + uuid; String msg = "Failed to delete application: " + uuid;
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }

@ -41,10 +41,118 @@ public class ConnectionManagerUtil {
private static ThreadLocal<TxState> currentTxState = new ThreadLocal<>(); private static ThreadLocal<TxState> currentTxState = new ThreadLocal<>();
private static DataSource dataSource; private static DataSource dataSource;
public static Connection getDBConnection() throws DBConnectionException {
Connection conn = currentConnection.get();
if (conn == null) {
try {
conn = dataSource.getConnection();
currentConnection.set(conn);
} catch (SQLException e) {
throw new DBConnectionException("Failed to get database connection.", e);
}
}
return conn;
}
public static void beginDBTransaction() throws TransactionManagementException, DBConnectionException {
Connection conn = currentConnection.get();
if (conn == null) {
conn = getDBConnection();
}
if (inTransaction(conn)) {
throw new IllegalTransactionStateException("Transaction has already been started.");
}
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new TransactionManagementException("Error occurred while starting a database transaction.", e);
}
}
public static void endDBTransaction() throws TransactionManagementException, DBConnectionException {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("Database connection is not active.");
}
if (!inTransaction(conn)) {
throw new IllegalTransactionStateException("Transaction has not been started.");
}
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
throw new TransactionManagementException("Error occurred while ending database transaction.", e);
}
}
public static void commitDBTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("Database connection is not active.");
}
if (!inTransaction(conn)) {
throw new IllegalTransactionStateException("Transaction has not been started.");
}
try {
conn.commit();
} catch (SQLException e) {
log.error("Error occurred while committing the transaction", e);
}
}
public static void rollbackDBTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("Database connection is not active.");
}
if (!inTransaction(conn)) {
throw new IllegalTransactionStateException("Transaction has not been started.");
}
try {
conn.rollback();
} catch (SQLException e) {
log.warn("Error occurred while roll-backing the transaction", e);
}
}
public static void closeDBConnection() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("Database connection is not active.");
}
try {
conn.close();
} catch (SQLException e) {
log.error("Error occurred while closing the connection", e);
}
currentConnection.remove();
}
private static boolean inTransaction(Connection conn) {
boolean inTransaction = true;
try {
if (conn.getAutoCommit()) {
inTransaction = false;
}
} catch (SQLException e) {
throw new IllegalTransactionStateException("Failed to get transaction state.");
}
return inTransaction;
}
@Deprecated
public static ThreadLocal<Connection> getCurrentConnection() { public static ThreadLocal<Connection> getCurrentConnection() {
return currentConnection; return currentConnection;
} }
@Deprecated
public static Connection openConnection() throws DBConnectionException { public static Connection openConnection() throws DBConnectionException {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { if (conn != null) {
@ -63,6 +171,7 @@ public class ConnectionManagerUtil {
return conn; return conn;
} }
@Deprecated
public static Connection getConnection() throws DBConnectionException { public static Connection getConnection() throws DBConnectionException {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
@ -73,6 +182,7 @@ public class ConnectionManagerUtil {
return conn; return conn;
} }
@Deprecated
public static void beginTransaction() throws TransactionManagementException, DBConnectionException { public static void beginTransaction() throws TransactionManagementException, DBConnectionException {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { if (conn != null) {
@ -102,6 +212,7 @@ public class ConnectionManagerUtil {
currentTxState.set(TxState.CONNECTION_BORROWED); currentTxState.set(TxState.CONNECTION_BORROWED);
} }
@Deprecated
public static void commitTransaction() { public static void commitTransaction() {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
@ -116,6 +227,7 @@ public class ConnectionManagerUtil {
} }
} }
@Deprecated
public static void rollbackTransaction() { public static void rollbackTransaction() {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
@ -130,6 +242,7 @@ public class ConnectionManagerUtil {
} }
} }
@Deprecated
public static void closeConnection() { public static void closeConnection() {
if(currentTxState != null) { if(currentTxState != null) {
TxState txState = currentTxState.get(); TxState txState = currentTxState.get();

Loading…
Cancel
Save