Skip to content

Commit

Permalink
GRAILS-6874 - support using the hibernate persistence interceptor in …
Browse files Browse the repository at this point in the history
…a nested fashion.

I suspect this was always the intended style of usage, but the implementation was bugged.
  • Loading branch information
ldaley committed Oct 26, 2010
1 parent 19d3808 commit 430bca8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Expand Up @@ -34,12 +34,13 @@ public class HibernatePersistenceContextInterceptor implements PersistenceContex
private static final Log LOG = LogFactory.getLog(HibernatePersistenceContextInterceptor.class);
private SessionFactory sessionFactory;
private boolean participate;
private int nestingCount = 0;

/* (non-Javadoc)
* @see org.codehaus.groovy.grails.support.PersistenceContextInterceptor#destroy()
*/
public void destroy() {
if (participate) {
if (--nestingCount > 0 || participate) {
return;
}

Expand Down Expand Up @@ -98,6 +99,9 @@ public boolean isOpen() {
* @see org.codehaus.groovy.grails.support.PersistenceContextInterceptor#init()
*/
public void init() {
if (++nestingCount > 1) {
return;
}
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
participate = true;
Expand Down
@@ -0,0 +1,62 @@
package org.codehaus.groovy.grails.orm.hibernate.support

import org.codehaus.groovy.grails.orm.hibernate.AbstractGrailsHibernateTests
import org.springframework.transaction.support.TransactionSynchronizationManager
import org.springframework.orm.hibernate3.SessionFactoryUtils
import org.springframework.orm.hibernate3.SessionHolder
import org.hibernate.Session
import org.hibernate.SessionFactory
import java.util.concurrent.CountDownLatch

/**
* @author Luke Daley
* @since 1.4
*/
class HibernatePersistenceContextInterceptorTests extends AbstractGrailsHibernateTests {

protected void setUp() {
super.setUp()

// We are going to manage the session ourselves through the interceptor
def holder = TransactionSynchronizationManager.getResource(sessionFactory)
def s = holder.session
TransactionSynchronizationManager.unbindResource(sessionFactory)
SessionFactoryUtils.releaseSession(s, sessionFactory)
}

void testSimpleLifecycle() {
def interceptor = getInterceptor()
assertEquals("interceptor open", false, interceptor.open)
interceptor.init()
assertEquals("interceptor open", true, interceptor.open)
interceptor.destroy()
assertEquals("interceptor open", false, interceptor.open)
interceptor.init()
assertEquals("interceptor open", true, interceptor.open)
interceptor.destroy()
assertEquals("interceptor open", false, interceptor.open)
}

void testNestedLifecycle() {
def interceptor = getInterceptor()
assertEquals("interceptor open", false, interceptor.open)

interceptor.init()
assertEquals("interceptor open", true, interceptor.open)
interceptor.init()
assertEquals("interceptor open", true, interceptor.open)
interceptor.destroy()
assertEquals("interceptor open", true, interceptor.open)
interceptor.destroy()
assertEquals("interceptor open", false, interceptor.open)

interceptor.init()
assertEquals("interceptor open", true, interceptor.open)
interceptor.destroy()
assertEquals("interceptor open", false, interceptor.open)
}

protected getInterceptor() {
appCtx.persistenceInterceptor
}
}

0 comments on commit 430bca8

Please sign in to comment.