0

In WildFly 14, I have a @Stateless bean that runs two selects with different connections/databases:

InitialContext context = new InitialContext();
DataSource ds = (DataSource)context.lookup("jndi/to/db/1");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from table1"); // table1 in db 1

.........
.........

InitialContext context2 = new InitialContext();
DataSource ds2 = (DataSource)context.lookup("jndi/to/db/2");
Connection conn2 = ds2.getConnection();  // <-- here it fails
Statement stmt2 = conn2.createStatement();
ResultSet rs2 = stmt2.executeQuery("select * from table2"); // table2 in db 2

I'm getting a warning in the second select (that also fails):

ARJUNA012140: Adding multiple last resources is disallowed

According to this link, the problem is that I'm running a single transaction with two different databases. Note that the datasources are NOT configured with the XA option, as there are no use cases in my application that update both databases in a single transaction.

The bean where I have the problem is read-only, it only executes two SQL selects.

I tried to annotate the bean method with:

@TransactionAttribute(javax.ejb.TransactionAttributeType.NEVER) 

But I get the runtime error:

WFLYEJB0063: Transaction present on server in Never call (EJB3 13.6.2.6)

Why is the transaction present? How to disable it?

ps0604
  • 1,227
  • 23
  • 133
  • 330

1 Answers1

1

Instead of @TransactionAttribute(javax.ejb.TransactionAttributeType.NEVER)

Use:

@TransactionAttribute(javax.ejb.TransactionAttributeType.NOT_SUPPORTED)

This makes the Stateless Bean to avoid attaching itself to the current running transaction; and run as if there is no transaction at all...

Why is the transaction present?

I really don't know... Did you invoke any other J2EE Component (for example, a CDI bean annotated with @Transactional, or other EJB) before calling your Stateless Bean?

Carlitos Way
  • 3,279
  • 20
  • 30
  • I ended up using `@TransactionManagement(TransactionManagementType.BEAN)` initiating the transactions manually with UserTransaction. The problem is that in the same bean some methods are read-only and others have updates, so I cannot use `NOT_SUPPORTED`. I still don't know why in CMT the `connect` methods start a transaction, I don't have `@Transactional` anywhere. – ps0604 Jan 20 '19 at 12:08