Class LoggingInterceptor

  • All Implemented Interfaces:
    Serializable, org.hibernate.Interceptor

    public class LoggingInterceptor
    extends org.hibernate.EmptyInterceptor
    Interceptor that attaches to Hibernate sessions to provide logging of items that are updated/created/deleted. Do not attach this interceptor to a SessionFactory since this class is not thread-safe.

    The interceptor uses onSave, onDelete and onFlushDirty to collect information about what has happened. The LogManagerFactory is asked to create EntityLogger:s for each modified/created/deleted item. The actual logging takes place in the postFlush method that calls EntityLogger.logChanges(LogManager, EntityDetails) on each entity logger.

    NOTE! For new items, the ID is not available until after the flush to the database has been done. Eg. new items has a 0 id when LogManagerFactory.getEntityLogger(LogManager, Object) is called.

    Version:
    2.13
    Author:
    Nicklas
    See Also:
    Serialized Form
    Last modified
    $Date: 2016-04-21 13:13:30 +0200 (to, 21 apr 2016) $
    • Constructor Detail

      • LoggingInterceptor

        public LoggingInterceptor​(LogControl logControl,
                                  ExtensionsInvoker<LogManagerFactory> logInvoker)
        Creates a new interceptor.
        Parameters:
        logControl - The LogControl that is managing the current transaction
        logInvoker - The extension invoker that create log manager factories
    • Method Detail

      • onSave

        public boolean onSave​(Object entity,
                              Serializable id,
                              Object[] state,
                              String[] propertyNames,
                              org.hibernate.type.Type[] types)
        Adds a CREATE event to the log.
        Specified by:
        onSave in interface org.hibernate.Interceptor
        Overrides:
        onSave in class org.hibernate.EmptyInterceptor
        Returns:
        Always false, since the state is not modified
      • onFlushDirty

        public boolean onFlushDirty​(Object entity,
                                    Serializable id,
                                    Object[] currentState,
                                    Object[] previousState,
                                    String[] propertyNames,
                                    org.hibernate.type.Type[] types)
        Adds an UPDATE event to the log.
        Specified by:
        onFlushDirty in interface org.hibernate.Interceptor
        Overrides:
        onFlushDirty in class org.hibernate.EmptyInterceptor
        Returns:
        Always false, since the state is not changed
      • onDelete

        public void onDelete​(Object entity,
                             Serializable id,
                             Object[] state,
                             String[] propertyNames,
                             org.hibernate.type.Type[] types)
        Adds a DELETE event to the log.
        Specified by:
        onDelete in interface org.hibernate.Interceptor
        Overrides:
        onDelete in class org.hibernate.EmptyInterceptor
      • postFlush

        public void postFlush​(Iterator entities)
        Collects all log events and sends them to the each EntityLogger.
        Specified by:
        postFlush in interface org.hibernate.Interceptor
        Overrides:
        postFlush in class org.hibernate.EmptyInterceptor
      • beforeTransactionCompletion

        public void beforeTransactionCompletion​(org.hibernate.Transaction tx)
        Flush remaining changes in the log control.
        Specified by:
        beforeTransactionCompletion in interface org.hibernate.Interceptor
        Overrides:
        beforeTransactionCompletion in class org.hibernate.EmptyInterceptor
      • afterTransactionCompletion

        public void afterTransactionCompletion​(org.hibernate.Transaction tx)
        Signals successful commit or rollback to the log manager. Note that this happens after the transaction/connection has been closed and no communication with the database can take place here.
        Specified by:
        afterTransactionCompletion in interface org.hibernate.Interceptor
        Overrides:
        afterTransactionCompletion in class org.hibernate.EmptyInterceptor
      • addManualSingleChange

        public void addManualSingleChange​(ChangeType action,
                                          Object entity,
                                          Object newValue,
                                          Object oldValue,
                                          String property,
                                          org.hibernate.type.Type type)
        Adds a manual non-Hibernate change to the log system. The change is forwarded to the log handlers immediately. The log handlers should not store the entity object for later use since instance may be reused for other changes in the future.
        Since:
        3.3
      • addManualSingleChange

        public void addManualSingleChange​(ChangeType action,
                                          Object entity)
        Adds a manual non-Hibernate change to the log system. The entity should be a special object that doesn't rely on the state arrays to convey what has changed. The change is forwarded to the log handlers immediately. The log handlers should not store the entity object for later use since instance may be reused for other changes in the future.
        Since:
        3.8
      • addChange

        private void addChange​(ChangeType action,
                               Object entity,
                               Object[] state,
                               Object[] previousState,
                               String[] propertyNames,
                               org.hibernate.type.Type[] types,
                               boolean logImmediately)
        If the log manager factory creates an entity logger, add information about the change to the internal list. The list is processed in the postFlush(Iterator) method.