[hibernate-dev] Getting notified when session factory is completely set up

Gunnar Morling gunnar at hibernate.org
Tue Feb 11 03:38:04 EST 2014


Hi,

Is there a way for services (living in the SF-scoped registry) to get
notified when the session factory has been set up and perform some action
using that completely initialized factory?

Upon invocation of initiateService() of SessionFactoryServiceInitiator
implementations, the passed factory is still under construction (the call
originates from SessionFactoryImpl<init>), so I can't access all members.
The same holds true for the StartStoppable contract in OGM which also sees
a session factory under construction.

Now I could store the passed reference in a field and execute the required
logic (some sort of validation) later on. I only have no meaningful hook to
trigger such action exactly once. So I'd have to manage a flag to make sure
the action is only performed once.

Is there a better way to achieve this?

Would it make sense to provide an additional contract to notify services
about session factory lifecycle events:

    public interface SessionFactoryLifecycleAware extends Service {

        void onSessionFactoryLifecycleEvent(SessionFactoryImplementor sfi,
Event event);

        public enum Event { POST_START, PRE_STOP }
    }

?

Thanks,

--Gunnar


More information about the hibernate-dev mailing list