Skip to content

Commit 30e85ec

Browse files
committed
Added more logs about the transaction
1 parent 3fb5b2a commit 30e85ec

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

dspace-api/src/main/java/org/dspace/core/HibernateDBConnection.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.hibernate.Transaction;
3131
import org.hibernate.engine.spi.SessionFactoryImplementor;
3232
import org.hibernate.proxy.HibernateProxyHelper;
33+
import org.hibernate.query.NativeQuery;
3334
import org.hibernate.resource.transaction.spi.TransactionStatus;
3435
import org.hibernate.stat.Statistics;
3536
import org.springframework.beans.factory.annotation.Autowired;
@@ -81,7 +82,11 @@ public Session getSession() throws SQLException {
8182
// If we don't yet have a live transaction, start a new one
8283
// NOTE: a Session cannot be used until a Transaction is started.
8384
if (!isTransActionAlive()) {
85+
log.info("getSession() - Starting new transaction");
86+
getTransactionPid();
8487
sessionFactory.getCurrentSession().beginTransaction();
88+
getTransactionPid();
89+
log.info("getSession() - New transaction started");
8590
configureDatabaseMode();
8691
}
8792
// Return the current Hibernate Session object (Hibernate will create one if it doesn't yet exist)
@@ -135,7 +140,11 @@ public boolean isSessionAlive() {
135140
@Override
136141
public void rollback() throws SQLException {
137142
if (isTransActionAlive()) {
143+
log.info("rollback() - rolling back transaction");
144+
getTransactionPid();
138145
getTransaction().rollback();
146+
getTransactionPid();
147+
log.info("rollback() - transaction rolled back");
139148
}
140149
}
141150

@@ -150,7 +159,11 @@ public void rollback() throws SQLException {
150159
@Override
151160
public void closeDBConnection() throws SQLException {
152161
if (sessionFactory.getCurrentSession() != null && sessionFactory.getCurrentSession().isOpen()) {
162+
log.info("closeDBConnection() - Closing current session");
163+
getTransactionPid();
153164
sessionFactory.getCurrentSession().close();
165+
log.info("closeDBConnection() - Session closed");
166+
getTransactionPid();
154167
}
155168
}
156169

@@ -170,8 +183,12 @@ public void commit() throws SQLException {
170183
TransactionStatus.ROLLING_BACK)) {
171184
// Flush synchronizes the database with in-memory objects in Session (and frees up that memory)
172185
getSession().flush();
186+
log.info("commit() - commiting transaction");
187+
getTransactionPid();
173188
// Commit those results to the database & ends the Transaction
174189
getTransaction().commit();
190+
getTransactionPid();
191+
log.info("commit() - transaction committed");
175192
}
176193
}
177194

@@ -235,7 +252,8 @@ public <E extends ReloadableEntity> E reloadEntity(final E entity) throws SQLExc
235252
return entity;
236253
} else {
237254
log.info("reloadEntity() - Entity is not in the session - return entity with ID {}", entity.getID());
238-
return (E) getSession().get(HibernateProxyHelper.getClassWithoutInitializingProxy(entity), entity.getID());
255+
return (E) getSession().get(HibernateProxyHelper.getClassWithoutInitializingProxy(entity),
256+
entity.getID());
239257
}
240258
} catch (Exception e) {
241259
log.error("reloadEntity() - Error reloading entity: {}", e.getMessage());
@@ -420,5 +438,23 @@ public String getHibernateStatistics() {
420438
return "SessionFactory is not available for logging Hibernate statistics.";
421439
}
422440
}
441+
442+
// Method to get the current transaction's PID from PostgreSQL
443+
public Integer getTransactionPid() {
444+
Integer pid = -1;
445+
try {
446+
Session session = sessionFactory.getCurrentSession(); // Get the current session
447+
String sql = "SELECT pg_backend_pid()"; // SQL query to get the PID
448+
449+
// Execute the query and get the PID
450+
NativeQuery<Integer> query = session.createNativeQuery(sql);
451+
pid = query.getSingleResult(); // Get the single result
452+
453+
log.info("Current transaction PID: " + pid); // Optional logging
454+
} catch (Exception e) {
455+
log.error("Cannot get PID because: " + e.getMessage());
456+
}
457+
return pid;
458+
}
423459
}
424460

dspace-server-webapp/src/main/java/org/dspace/app/rest/filter/DSpaceRequestContextFilter.java

+5
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
5050
} finally {
5151
// Abort the context if it's still valid, thus closing any open
5252
// database connections
53+
log.info("DSpaceRequestContextFilter: Aborting Context - finally block");
5354
if ((context != null) && context.isValid()) {
55+
log.info("DSpaceRequestContextFilter: Aborting Context");
5456
ContextUtil.abortContext(request);
57+
log.info("DSpaceRequestContextFilter: Context Aborted");
58+
} else {
59+
log.info("DSpaceRequestContextFilter: Context was already null or invalid");
5560
}
5661
}
5762
}

0 commit comments

Comments
 (0)