Issue Details (XML | Word | Printable)

Key: HIVEMIND-54
Type: New Feature New Feature
Status: Resolved Resolved
Resolution: Won't Fix
Priority: Major Major
Assignee: Howard M. Lewis Ship
Reporter: Yuxiang Bu
Votes: 1
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
HiveMind

Request for built-in support for Hibernate in HiveMind lib

Created: 13/Sep/04 09:24 AM   Updated: 06/May/06 10:53 PM
Return to search
Component/s: documentation, examples, hivebuild
Affects Version/s: None
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments:
  Size
Zip Archive Licensed for inclusion in ASF works doconline.zip 2004-09-14 04:51 PM Yuxiang Bu 17 kB
Zip Archive Licensed for inclusion in ASF works hivemind-article.zip 2004-10-03 11:51 PM James Carman 48 kB
GZip Archive Licensed for inclusion in ASF works hivemind-hibernate.tar.gz 2004-09-13 02:15 PM Jimmi Dyson 5 kB
Zip Archive Licensed for inclusion in ASF works hivemind.hibernate.zip 2004-10-20 03:03 PM Jean-Francois Poilpret 144 kB

Resolution Date: 06/May/06 10:53 PM


 Description  « Hide
Request for adding build-in support for Hibernate Session and Transaction management in HiveMind lib.

Since Hibernate is so popular now, I believe it will become a hot request.

At first I try to develop an HibernateInterceptorFactory service using javassist.
But I found javassist cannot support "finally" yet. And the target code like this:

    boolean isTransactionBegunInThisMethod = false;
    try {
        isTransactionBegunInThisMethod = tm.beginTransaction();
        Object result = _inner....
        if (isTransactionBegunInThisMethod)
            tm.commitTransaction();
        return result;
    } finally {
        if (isTransactionBegunInThisMethod)
            tm.rollbackTransaction();
    }

And I cannot develop similar codes in javassist.
Then I developed an Interceptor with JDK proxy.

However, the interceptor cannot be used after HiveMind LoggingInterceptor.
The Registry successfully applies my HibernateInterceptor to service but fail to apply the Logging Interceptor.

org.apache.hivemind.ApplicationRuntimeException: Unable to construct service doconline.Adder: Unable to lookup $Proxy0: $Proxy0
at org.apache.hivemind.impl.servicemodel.AbstractServiceModelImpl.constructNewServiceImplementation(AbstractServiceModelImpl.java:156)
......
Caused by: org.apache.hivemind.ApplicationRuntimeException: Unable to lookup $Proxy0: $Proxy0
at org.apache.hivemind.service.impl.CtClassSource.getCtClass(CtClassSource.java:60)
at org.apache.hivemind.service.impl.ClassFabImpl.addField(ClassFabImpl.java:71)
at org.apache.hivemind.service.impl.LoggingInterceptorFactory.createInfrastructure(LoggingInterceptorFactory.java:236)
......
Caused by: javassist.NotFoundException: $Proxy0
at javassist.ClassPoolTail.openClassfile(ClassPoolTail.java:300)
at javassist.ClassPoolTail.checkClassName(ClassPoolTail.java:177)
at javassist.ClassPool.checkClassName(ClassPool.java:709)
at javassist.ClassPool.get0(ClassPool.java:572)
at javassist.ClassPool.get(ClassPool.java:561)
at org.apache.hivemind.service.impl.CtClassSource.getCtClass(CtClassSource.java:56)
... 48 more

The problem seems occurs when javassist want to find the class file in classpath but cannot find it( Of course it cannot ).

If I put Transaction Interceptor before the Logging Interceptor, all works well.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Jimmi Dyson added a comment - 13/Sep/04 02:15 PM
Here is something I've just finished to do this exact thing. Maybe it will be useful, maybe not. I have created a transaction interceptor and a threaded Hibernate Session proxy. Take a look at the attached code.

Works with logging interceptor before of after the TransactionIinterceptor.

Regards,
Jim Dyson

Yuxiang Bu added a comment - 14/Sep/04 05:06 PM
Thanks Jimmi. I found I made a mistake : The "finally" is in fact unnecessary and use "catch" is enough. So I rewrite my code with javassist and upload it as attachment.

My approach is a little different from yours. I didn't create SessionFactoryProxy service but just wrap the SessionFactory in a SessionSource service, whose only method is a getSession() method.
The business logic services will depend on this service and their business methods just call the getSession() method at start.
The Session is stored in ThreadLocalStorage service and will be closed when the thread is cleaned up.

Also I guess it will cause some defect that TransactionInterceptor can only apply to Session.save/update/delete methods.
Look at the sample code:

List l = session.find("from Foo");
Foo f = (Foo)l.get(0);
f.setName( someName );

These codes didn't call any save/update/delete methods. But it really changes the database status and should be put under transaction context. So my TransactionInterceptor can apply to any method.

And at last, I know my sourcecode is not a patch since I didn't create any Testcase or document. I just hope these litle codes can be helpful to someone.

Jimmi Dyson added a comment - 15/Sep/04 10:01 AM
Good stuff. it certainly is a defect of mine to only be able to use the interceptor on subinterfaces of Session, but I couldn't think of another way to get hold of the session to be able to begin/commit/rollback the transaction. Using the ThreadLocalStorage is something I didn't think of.

I think that you should use the SessionFactoryProxy - this enables you to have multiple SessionFactories and hence produce connections to multiple databases. You can specify a different hibernate config file for each SessionFactory rather than using the default name as you do.

Another problem is that by using the ThreadLocalStorage with a single named key, you're limiting the thread to one database connection per thread. By using a threaded model, as I do in the SessionProxy, you're not. But that creates problems in getting the Session in TransactionManagerImpl, as it would mean that the SessionSource wasn't a singleton anymore.

Yuxiang Bu added a comment - 15/Sep/04 12:59 PM
I have no experience in multi-database programming. So I never consider how to configure Hibernate with multi-database. You must work on some large project. For my project, one database and one SessionFactory is enough.
Anyway, multi-database can be configured in one XADataSource. But I don't know whether Hibernate supports XADataSource, or how to manage the transaction in Hibernate. Maybe JTA will handle it...

Howard M. Lewis Ship added a comment - 15/Sep/04 01:04 PM
Might be easier to discuss this as a Change Proposal on the Wiki.

I just fixed a bug (HIVEMIND-55) related to JDK proxies (that problem you observed), so you should be able to order your interceptors appropriately. The change is in CVS HEAD and will be in the 1.0 final release.

James Carman added a comment - 15/Sep/04 04:21 PM
I think we need to take a step back and look at a bigger picture. We shouldn't concentrate upon making hibernate-specific services. We should work to provide a general framework for DAOs (a la Spring). In my article, I created a generalized TransactionServices (begin, commit, rollback, rollbackOnly, etc.) which is used by a transaction coordinator (or manager). Now, in a hibernate environment, I'll use a hibernate-specific version of the TransactionServices interface. With JDO, I'll use a JDO-specific version. This is where we should lean, IMHO.

Marcus Brito added a comment - 16/Sep/04 12:36 PM
I'm worried about duplication of effort here. Couldn't we leverage what's already in the wild (read: spring)? Things like transaction demarcation, jdbc and orm support are battered subjects and we should try to integrate what's already available with Hivemind.

As Howard is fond of saying, Hivemind is object soup. Can't we just throw in those spring object/classes instead of cooking everything again?

James Carman added a comment - 16/Sep/04 12:55 PM
I think we could leverage some of the ideas in Spring, but HiveMind does have one interesting feature, service models, which makes implementing this stuff MUCH easier than the way they do it with Spring. We don't NEED thread local variables in HiveMind. We can use pooled/threaded service models!

James Carman added a comment - 03/Oct/04 11:51 PM
Hey, guys. Here's a sneak peak of the code that I'm writing for TSS.com about HiveMind. It contains code that I've been working on which integrates Hibernate and HiveMind. I would love to have any suggestions, comments. This zip file contains an Ant build file. You should modify the build.properties file (database settings, deploy location, etc) if you want to actually install and run the example webapp. You'll need to run the "get-dependencies" task first (hopefully you're not behind a proxy server, because I didn't account for that yet). This is an example of what I have in mind with respect to support for Hibernate in HiveMind. Of course, we might want to change the names of some of the interfaces/classes (to protect the innocent). One thing I don't particularly like is the duplication of code between the TransactionInterceptor (a service interceptor) and TransactionFilter (a servlet filter) classes, but I think it's the easiest way to implement what we need without adding new classes/interfaces. I created a class called HibernateSessionWrapper which wraps a Hibernate session and provides many of the common operations you might need WITHOUT the checked exceptions (they throw runtime exceptions instead). Anyway, please check it out and let me know what you think.

Jean-Francois Poilpret added a comment - 20/Oct/04 03:03 PM
Here is the current status of some work I have done to integrate Hibernate in Hivemind in a general way (general Transaction Service).

Hope it can help someone.

James Carman added a comment - 17/Aug/05 10:59 PM
I'm +1 for closing this issue. Hibernate-specific stuff should go into external helper libraries like hiveutils, or whatever Jean-Francois is calling it now. :-) He's done a lot of cool work in that project that others could leverage.

James Carman added a comment - 06/May/06 10:53 PM
Hibernate-specific facilities are outside the scope of the HiveMind project. Please see either hivetranse or the hivemind project at JavaForge for Hibernate support.