30
30
import org .hibernate .Transaction ;
31
31
import org .hibernate .engine .spi .SessionFactoryImplementor ;
32
32
import org .hibernate .proxy .HibernateProxyHelper ;
33
+ import org .hibernate .query .NativeQuery ;
33
34
import org .hibernate .resource .transaction .spi .TransactionStatus ;
34
35
import org .hibernate .stat .Statistics ;
35
36
import org .springframework .beans .factory .annotation .Autowired ;
@@ -81,7 +82,11 @@ public Session getSession() throws SQLException {
81
82
// If we don't yet have a live transaction, start a new one
82
83
// NOTE: a Session cannot be used until a Transaction is started.
83
84
if (!isTransActionAlive ()) {
85
+ log .info ("getSession() - Starting new transaction" );
86
+ getTransactionPid ();
84
87
sessionFactory .getCurrentSession ().beginTransaction ();
88
+ getTransactionPid ();
89
+ log .info ("getSession() - New transaction started" );
85
90
configureDatabaseMode ();
86
91
}
87
92
// Return the current Hibernate Session object (Hibernate will create one if it doesn't yet exist)
@@ -135,7 +140,11 @@ public boolean isSessionAlive() {
135
140
@ Override
136
141
public void rollback () throws SQLException {
137
142
if (isTransActionAlive ()) {
143
+ log .info ("rollback() - rolling back transaction" );
144
+ getTransactionPid ();
138
145
getTransaction ().rollback ();
146
+ getTransactionPid ();
147
+ log .info ("rollback() - transaction rolled back" );
139
148
}
140
149
}
141
150
@@ -150,7 +159,11 @@ public void rollback() throws SQLException {
150
159
@ Override
151
160
public void closeDBConnection () throws SQLException {
152
161
if (sessionFactory .getCurrentSession () != null && sessionFactory .getCurrentSession ().isOpen ()) {
162
+ log .info ("closeDBConnection() - Closing current session" );
163
+ getTransactionPid ();
153
164
sessionFactory .getCurrentSession ().close ();
165
+ log .info ("closeDBConnection() - Session closed" );
166
+ getTransactionPid ();
154
167
}
155
168
}
156
169
@@ -170,8 +183,12 @@ public void commit() throws SQLException {
170
183
TransactionStatus .ROLLING_BACK )) {
171
184
// Flush synchronizes the database with in-memory objects in Session (and frees up that memory)
172
185
getSession ().flush ();
186
+ log .info ("commit() - commiting transaction" );
187
+ getTransactionPid ();
173
188
// Commit those results to the database & ends the Transaction
174
189
getTransaction ().commit ();
190
+ getTransactionPid ();
191
+ log .info ("commit() - transaction committed" );
175
192
}
176
193
}
177
194
@@ -235,7 +252,8 @@ public <E extends ReloadableEntity> E reloadEntity(final E entity) throws SQLExc
235
252
return entity ;
236
253
} else {
237
254
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 ());
239
257
}
240
258
} catch (Exception e ) {
241
259
log .error ("reloadEntity() - Error reloading entity: {}" , e .getMessage ());
@@ -420,5 +438,23 @@ public String getHibernateStatistics() {
420
438
return "SessionFactory is not available for logging Hibernate statistics." ;
421
439
}
422
440
}
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
+ }
423
459
}
424
460
0 commit comments