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

4.x.x
megala21 7 years ago
commit 2ab43c89ac

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

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

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

@ -41,10 +41,118 @@ public class ConnectionManagerUtil {
private static ThreadLocal<TxState> currentTxState = new ThreadLocal<>();
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() {
return currentConnection;
}
@Deprecated
public static Connection openConnection() throws DBConnectionException {
Connection conn = currentConnection.get();
if (conn != null) {
@ -63,6 +171,7 @@ public class ConnectionManagerUtil {
return conn;
}
@Deprecated
public static Connection getConnection() throws DBConnectionException {
Connection conn = currentConnection.get();
if (conn == null) {
@ -73,6 +182,7 @@ public class ConnectionManagerUtil {
return conn;
}
@Deprecated
public static void beginTransaction() throws TransactionManagementException, DBConnectionException {
Connection conn = currentConnection.get();
if (conn != null) {
@ -102,6 +212,7 @@ public class ConnectionManagerUtil {
currentTxState.set(TxState.CONNECTION_BORROWED);
}
@Deprecated
public static void commitTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
@ -116,6 +227,7 @@ public class ConnectionManagerUtil {
}
}
@Deprecated
public static void rollbackTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
@ -130,6 +242,7 @@ public class ConnectionManagerUtil {
}
}
@Deprecated
public static void closeConnection() {
if(currentTxState != null) {
TxState txState = currentTxState.get();

Loading…
Cancel
Save