Skip to content

Commit

Permalink
Allow accessing the ClientSession programmatively
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Jan 2, 2023
1 parent bd66b86 commit fa29a6e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,23 +320,27 @@ ClientSession getSession(Object entity) {
return getSession(entity.getClass());
}

ClientSession getSession(Class<?> entityClass) {
public ClientSession getSession(Class<?> entityClass) {
ClientSession clientSession = null;
MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class);
InstanceHandle<TransactionSynchronizationRegistry> instance = Arc.container()
.instance(TransactionSynchronizationRegistry.class);
if (instance.isAvailable()) {
TransactionSynchronizationRegistry registry = instance.get();
if (registry.getTransactionStatus() == Status.STATUS_ACTIVE) {
clientSession = (ClientSession) registry.getResource(SESSION_KEY);
if (clientSession == null) {
MongoEntity mongoEntity = entityClass == null ? null : entityClass.getAnnotation(MongoEntity.class);
return registerClientSession(mongoEntity, registry);
}
}
}
return clientSession;
}

public ClientSession getSession() {
return getSession(null);
}

private ClientSession registerClientSession(MongoEntity mongoEntity,
TransactionSynchronizationRegistry registry) {
TransactionManager transactionManager = Arc.container().instance(TransactionManager.class).get();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.mongodb.panache;

import com.mongodb.session.ClientSession;
import io.quarkus.mongodb.panache.runtime.JavaMongoOperations;

public class Panache {

public static ClientSession getSession() {
return JavaMongoOperations.INSTANCE.getSession();
}

public static ClientSession getSession(Class entityClass) {
return JavaMongoOperations.INSTANCE.getSession(entityClass);
}
}

0 comments on commit fa29a6e

Please sign in to comment.