-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: session.connection and transactions
PostPosted: Tue Jul 06, 2004 10:27 am 
Newbie

Joined: Tue Jul 06, 2004 9:56 am
Posts: 14
Hello,

i am trying to implement an audit log for the business objects in my database. Each business table has its own log table. Now, I've seen the AuditLog / History pattern in the Wiki, yet I got a problem with it when implementing the interceptor pattern: I cannot use the session but I can use a second session that sits on the same JDBC connection (see thread there):
secondSession = sessionFactory.openSession(currentSession.connection())

Now, when I open a transaction, change some business objects, then commit using transaction.commit() from within the application, can I safely(!!) call save(log) from within Interceptor.onFlushDirty() using the second session? And, most importantly, do the create-statements go in the same transaction as the business object updates? (because if they don't the whole interceptor thingy does not work for me)

A second (working) solution is using Lifecycle (onLoad and onSave,...) to register the new/loaded.. objects, then overriding transaction.commit to first dirty-check the registered objects and then saving the new or dirty ones into the log tables before actually commit(). Down-side: manual dirty-checking (which is then doubled since Hibernate does it too, but as it seems, I cannot access this information outside an interceptor).

Thanks, Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 12:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yes it is safe and the same tx will be used

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 1:54 pm 
Newbie

Joined: Tue Jul 06, 2004 9:56 am
Posts: 14
Thanks for the quick answer. I tried it but it seems not to go into the same transaction (Hibernate 2.1.3), at least not the way I do it:

Session / interceptor creation:
Code:
AuditLogInterceptor i = new AuditLogInterceptor(userId);
session = this.sessionFactory.openSession(i);
i.setAuditLogSession(sessionFactory.openSession(session.connection());
Transaction tx = session.beginTransaction();
...


AuditLogInterceptor: onFlushDirty() / onSave()
Code:
...
auditLogSession.save(logObject);
auditLogSession.flush(); // needed otherwise save() is ignored
...

The auditLogSession is closed when the (primary) session is closed.

Two things that indicate that I am missing the transaction:
1) I need to flush() the second session after each save() otherwise the log objects are not created in the database. This should not be necessary.
2) If the transaction fails and is rolled back the (flushed) log object remains in the database.

Same is true for onSave() (which I need too, of course).

What am I missing?

Thank you in advance,
Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 4:56 pm 
Newbie

Joined: Tue Jul 06, 2004 9:56 am
Posts: 14
Hm, now using a combination of interceptor (to set the user/rationale, on the same object/table) and database triggers in order to circumvent any trouble Hibernate could possibly make :-) As it was mentioned in one another thread, this is probably the best solution anyway.

Yet, I am still curious thus the question remains :-)

Regards,
Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 12, 2004 12:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Diederich wrote:
Two things that indicate that I am missing the transaction:
1) I need to flush() the second session after each save() otherwise the log objects are not created in the database. This should not be necessary.
2) If the transaction fails and is rolled back the (flushed) log object remains in the database.

1. flushing the session is only done if you use the Hibernate Transaction API, this is obviously not the case in your 2nd session
2. Hum, this is strange because it only delegates to plain JDBC or JTA, did you start the transaction before logging?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 3:47 am 
Newbie

Joined: Tue Jul 06, 2004 9:56 am
Posts: 14
emmanuel wrote:
2. Hum, this is strange because it only delegates to plain JDBC or JTA, did you start the transaction before logging?

I first got the second connection and assigned it to the interceptor. After that, I started the transaction, then come the usual data manipulations, then the transaction commit (which is when the interceptor is called, I suppose).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.