Hi,
No it does work with triggers.
It took me half a day of strugling to find out how it worked but ....
In Oracle you can access the sys.v_$session view from triggers. This view has a client_identifier column. This column can be accessed in a J2ee application by invoking the setClientIdentifier method on the OracleConnection object. This way you can pass j2ee usernames to the database. Vendor specific as hell, but allowed at my site.
The hardest part was to invoke the Oracle connection from a hibernate session. In the end I did it like this but I don't know if this is the proper way. Is there a better way to invoke the connection?
Code:
Connection connection = sessionFactory.getCurrentSession().connection();
Proxy l_connectionProxy = (Proxy)connection;
InvocationHandler l_invocationHandler = Proxy.getInvocationHandler(l_connectionProxy);
l_invocationHandler.invoke(l_connectionProxy,OracleConnection.class.getMethod("setClientIdentifier",new Class[]{String.class}),new Object[]{username});
The code for the trigger is:
Code:
CREATE OR REPLACE TRIGGER DAO.AUDIT_TRIGGER
BEFORE DELETE OR INSERT OR UPDATE
ON DAO.PERSON
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
tmpVar sys.v_$session.CLIENT_IDENTIFIER%type;
BEGIN
SELECT ses.client_identifier INTO tmpVar FROM sys.v_$session ses where ses.audsid = userenv('sessionid');
:new.cid := tmpVar;
END ;
person is the table that is audited and cid is the column for the username. The user has to have select rights on the v-$session view.
Joris