Saturday, August 15, 2015

Trace All opened sessions on hibernate

For bench marking issues we needed to trace all opened sessions by hibernate.
We were also tracing when it was closed and which objects were loaded during session.
Idea was simple.


Exception ex = new Exception( "Session opened on " + CalendarUtils.now( ) ) ;


This uniquely identifies the current path of execution and since date is added it is unique.



Example : below code puts a mark a debug object on transaction trace pool. So that we can find
when begined the problematic transaction began.



 public static void beginTransaction( )
    {
        getSession( ).beginTransaction( ) ;

        Exception ex = new Exception( "Transaction began on " + CalendarUtils.now( ) ) ;

        Integer code = getSessionStackTraceIdent( ).get( ) ;

        getTransactionTracePool( ).put( code, ex ) ;

        getTransactionTimestampPool( ).put( code, CalendarUtils.now( ) ) ;

        System.err.println( "Transaction Open in beginTransaction " + getSession( ).getTransaction( ).hashCode( ) ) ;
    }
 

No comments:

Post a Comment