Skip to content

Commit

Permalink
Set a JNDI Reference in the ConnectionFactory to avoid serializat…
Browse files Browse the repository at this point in the history
…ion and JNDI lookups

Since the original implementation of the `ConnectionFactory` may require serialization and JNDI lookups, setting a simple `Reference` here would avoid these issues
  • Loading branch information
gastaldi committed Feb 3, 2025
1 parent 316a236 commit cc611e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkiverse.ironjacamar;

import javax.naming.Referenceable;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.jms.ConnectionFactory;
Expand Down Expand Up @@ -85,4 +87,10 @@ public boolean isNotTransacted() {
}
}

@GET
@Path("/ref")
public String getRef() throws Exception {
return "ref=" + ((Referenceable) factory).getReference();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import jakarta.transaction.Transactional;

Expand Down Expand Up @@ -45,6 +46,11 @@ public void testTransacted() {
given().when().get("/jca/transacted").then().statusCode(200).body(is("true"));
}

@Test
public void testRef() {
given().when().get("/jca/ref").then().statusCode(200).body(is(not("ref=null")));
}

@Test
@Disabled("JMSContext.getTransacted() returns true even if the transaction is not active")
public void testNotTransacted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import java.util.function.Function;
import java.util.function.Supplier;

import javax.naming.Reference;

import jakarta.enterprise.inject.spi.DeploymentException;
import jakarta.resource.Referenceable;
import jakarta.resource.ResourceException;

import org.eclipse.microprofile.context.ManagedExecutor;
Expand Down Expand Up @@ -78,7 +81,12 @@ public Function<SyntheticCreationalContext<Object>, Object> createConnectionFact
IronJacamarContainer container = context.getInjectedReference(IronJacamarContainer.class,
Identifier.Literal.of(id));
try {
return container.createConnectionFactory();
Object connectionFactory = container.createConnectionFactory();
if (connectionFactory instanceof Referenceable ref) {
// Set a reference to avoid serialization and JNDI lookups. See https://issues.apache.org/jira/browse/ARTEMIS-5253
ref.setReference(new Reference(connectionFactory.getClass().getCanonicalName(), null, null));
}
return connectionFactory;
} catch (ResourceException e) {
throw new DeploymentException("Cannot create connection factory", e);
}
Expand Down

0 comments on commit cc611e2

Please sign in to comment.