Wednesday, January 11, 2012

Overriding prepareSession on Application Modules


Had a case where an overridden prepareSession method in an Application Module Implementation class was not firing each time a new database session was acquired by the Application Module.

The method was only firing the first time the Application Module was instantiated, however the prepareSession method in the super was firing each time as expected.  As a result, database session data which was being set in the prepareSession method was not always available on the database session.

After some time I realised that the wrong prepareSession method was overridden.  There are 2 available in for overriding, the difference being the parameters - Session vs SessionData.  Check carefully when choosing which to override, this could cause unexpected behaviour and be time consuming to debug when the wrong one is used:

Fig. 1

To do something for each database session retrieved by the Application Module, you should only ever use the one highlighted in Fig. 1.  There is not much oracle documentation available on the differences between the two.  Even in Chapter 9.11.3 of the Developer Guide they just refer to the method as prepareSession() in the text.  In the example code though, you will notice that they use prepareSession(Session session).

Also, if you check the javadocs for ApplicationModuleImpl, you will see it is suggested to never use prepareSession(SessionData sessionData) in your application: