diff --git a/querydsl-lucene/pom.xml b/querydsl-lucene/pom.xml
index e4efd45db..c612a4018 100644
--- a/querydsl-lucene/pom.xml
+++ b/querydsl-lucene/pom.xml
@@ -49,7 +49,14 @@
${project.parent.version}
test
test-jar
-
+
+
+
+ org.springframework
+ spring-aop
+ 3.0.3.RELEASE
+ test
+
diff --git a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneTransactionHandler.java b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneTransactionHandler.java
deleted file mode 100644
index 5ab5aaf04..000000000
--- a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneTransactionHandler.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package com.mysema.query.lucene.session;
-
-import org.aspectj.lang.ProceedingJoinPoint;
-import org.aspectj.lang.annotation.Around;
-import org.aspectj.lang.annotation.Aspect;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.mysema.query.lucene.session.impl.LuceneSessionFactoryImpl;
-import com.mysema.query.lucene.session.impl.LuceneSessionHolder;
-
-@Aspect
-public class LuceneTransactionHandler {
-
- private final Logger logger = LoggerFactory.getLogger(LuceneTransactionHandler.class);
-
- private LuceneSessionFactoryImpl sessionFactory;
-
- public LuceneTransactionHandler(LuceneSessionFactoryImpl sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- @Around("@annotation(luceneTransactionAnnotation)")
- public Object transactionalMethod(ProceedingJoinPoint joinPoint, LuceneTransaction annotation)
- throws Throwable {
-
- if (!LuceneSessionHolder.hasCurrentSession()) {
-
- if (logger.isDebugEnabled()) {
- logger.debug("Binding new session to thread");
- }
-
- LuceneSession session = sessionFactory.openSession(annotation.readOnly());
- LuceneSessionHolder.setCurrentSession(session);
- }
-
- LuceneSessionHolder.lease();
- try {
- return joinPoint.proceed();
- } finally {
- LuceneSessionHolder.release();
- }
-
- }
-
-}
diff --git a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/impl/LuceneSessionFactoryImpl.java b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/impl/LuceneSessionFactoryImpl.java
index 763dd2713..fa5dd19ac 100644
--- a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/impl/LuceneSessionFactoryImpl.java
+++ b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/impl/LuceneSessionFactoryImpl.java
@@ -4,6 +4,7 @@ import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
+import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
@@ -13,6 +14,7 @@ import org.slf4j.LoggerFactory;
import com.mysema.query.QueryException;
import com.mysema.query.lucene.session.LuceneSession;
import com.mysema.query.lucene.session.LuceneSessionFactory;
+import com.mysema.query.lucene.session.NoSessionBoundException;
public class LuceneSessionFactoryImpl implements LuceneSessionFactory {
@@ -22,6 +24,21 @@ public class LuceneSessionFactoryImpl implements LuceneSessionFactory {
private final AtomicReference searcher = new AtomicReference();
+ private LuceneInternalsFactory intFactory = new LuceneInternalsFactory() {
+ public LuceneSearcher createSearcher() throws CorruptIndexException, IOException {
+ return new LuceneSearcher(new IndexSearcher(directory));
+ }
+
+ public LuceneWriterImpl createWriter(boolean createNew) {
+ return new LuceneWriterImpl(directory, createNew);
+ }
+ };
+
+ public static interface LuceneInternalsFactory {
+ LuceneSearcher createSearcher() throws CorruptIndexException, IOException;
+ LuceneWriterImpl createWriter(boolean createNew);
+ }
+
public LuceneSessionFactoryImpl(String indexPath) throws IOException {
File folder = new File(indexPath);
if (!folder.exists() && !folder.mkdirs()) {
@@ -43,7 +60,21 @@ public class LuceneSessionFactoryImpl implements LuceneSessionFactory {
@Override
public LuceneSession getCurrentSession() {
- return LuceneSessionHolder.getCurrentSession();
+ if (!LuceneSessionHolder.isTransactionalScope()) {
+ throw new NoSessionBoundException("There is transactional scope");
+ }
+
+ if (!LuceneSessionHolder.hasCurrentSession(this)) {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("Binding new session to thread");
+ }
+
+ LuceneSession session = openSession(LuceneSessionHolder.getReadOnly());
+ LuceneSessionHolder.setCurrentSession(this, session);
+ }
+
+ return LuceneSessionHolder.getCurrentSession(this);
}
@Override
@@ -52,7 +83,7 @@ public class LuceneSessionFactoryImpl implements LuceneSessionFactory {
}
public LuceneWriterImpl getWriter(boolean createNew) {
- return new LuceneWriterImpl(directory, createNew);
+ return intFactory.createWriter(createNew);
}
public LuceneSearcher leaseSearcher() {
@@ -91,7 +122,7 @@ public class LuceneSessionFactoryImpl implements LuceneSessionFactory {
}
private LuceneSearcher createNewSearcher(LuceneSearcher expected) throws IOException {
- LuceneSearcher s = new LuceneSearcher(new IndexSearcher(directory));
+ LuceneSearcher s = intFactory.createSearcher();
if (!searcher.compareAndSet(expected, s)) {
// Some thread already created a new one so just close this
s.release();
diff --git a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/impl/LuceneSessionHolder.java b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/impl/LuceneSessionHolder.java
index 47b2a058e..63585de96 100644
--- a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/impl/LuceneSessionHolder.java
+++ b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/impl/LuceneSessionHolder.java
@@ -1,66 +1,101 @@
package com.mysema.query.lucene.session.impl;
-import com.mysema.query.lucene.session.LuceneSession;
-import com.mysema.query.lucene.session.NoSessionBoundException;
+import java.util.HashMap;
+import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.mysema.commons.lang.Assert;
+import com.mysema.query.QueryException;
+import com.mysema.query.lucene.session.LuceneSession;
+import com.mysema.query.lucene.session.LuceneSessionFactory;
/**
- * Holds the thread local session
+ * Holds the thread local sessions. This can handle several session factories
+ * per thread.
*
- * @author laimw
+ * @author laim
*/
public final class LuceneSessionHolder {
- private static final ThreadLocal currentSessionRef =
- new ThreadLocal();
+ private static final Logger logger = LoggerFactory.getLogger(LuceneSessionHolder.class);
- private static class LuceneSessionRef {
- private LuceneSession session;
+ private static final ThreadLocal