Skip to content

Commit

Permalink
Added more logs to context
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Dec 17, 2024
1 parent 56b4862 commit 6f25873
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
43 changes: 30 additions & 13 deletions dspace-api/src/main/java/org/dspace/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public Context(Mode mode) {
* Initializes a new context object.
*/
protected void init() {
try {
updateDatabase();

if (eventService == null) {
Expand Down Expand Up @@ -197,7 +198,10 @@ protected void init() {
if (this.mode != null) {
setMode(this.mode);
}

} catch (Exception e) {
log.error("Error initializing Context", e);
throw e; // Fail fast if initialization cannot be completed
}
}

/**
Expand Down Expand Up @@ -392,23 +396,27 @@ public String getExtraLogInfo() {
* or closing the connection
*/
public void complete() throws SQLException {
// If Context is no longer open/valid, just note that it has already been closed
if (!isValid()) {
log.info("complete() was called on a closed Context object. No changes to commit.");
return;
}

try {
// As long as we have a valid, writeable database connection,
// commit changes. Otherwise, we'll just close the DB connection (see below)
if (!isReadOnly()) {
commit();
commit(); // Commit the transaction
}
} catch (Exception e) {
log.error("Error committing transaction in complete()", e);
throw e; // Rethrow to signal failure to higher-level logic
} finally {
if (dbConnection != null) {
// Free the DB connection and invalidate the Context
dbConnection.closeDBConnection();
dbConnection = null;
try {
dbConnection.closeDBConnection();
log.info("Database connection closed after complete().");
dbConnection = null;
} catch (SQLException ex) {
log.error("Error closing the database connection after complete()", ex);
}
}
}
}
Expand Down Expand Up @@ -568,8 +576,11 @@ public void rollback() throws SQLException {
dbConnection.rollback();
reloadContextBoundEntities();
}
} catch (SQLException e) {
log.error("Error rolling back transaction", e);
throw e; // Signal failure to the caller
} finally {
events = null;
events = null; // Clear events
}
}

Expand All @@ -594,6 +605,7 @@ public void abort() {
// Rollback ONLY if we have a database transaction, and it is NOT Read Only
if (!isReadOnly() && isTransactionAlive()) {
dbConnection.rollback();
log.info("Transaction successfully rolled back during abort().");
}
} catch (SQLException se) {
log.error("Error rolling back transaction during an abort()", se);
Expand All @@ -602,12 +614,13 @@ public void abort() {
if (dbConnection != null) {
// Free the DB connection & invalidate the Context
dbConnection.closeDBConnection();
log.info("Database connection closed during abort().");
dbConnection = null;
}
} catch (Exception ex) {
log.error("Error closing the database connection", ex);
log.error("Error closing the database connection during abort()", ex);
}
events = null;
events = null; // Clear events to release resources
}
}

Expand All @@ -617,8 +630,12 @@ public void abort() {
*/
@Override
public void close() {
if (isValid()) {
abort();
try {
if (isValid()) {
abort();
}
} catch (Exception e) {
log.error("Error during context closure", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected void handleSQLException(HttpServletRequest request, HttpServletRespons
@ExceptionHandler(IOException.class)
protected void handleIOException(HttpServletRequest request, HttpServletResponse response, Exception ex)
throws IOException {
log.error("Custom log: An internal read or write operation failed", ex);
sendErrorResponse(request, response, ex,
"An internal read or write operation failed",
HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
Expand Down

0 comments on commit 6f25873

Please sign in to comment.