Subject: [pgjdbc/pgjdbc] PR #3897: fix: issue #3892, PGXAConnection.prepare(Xid) should return XA_RDONLY if the connection is read only

From: "davecramer (@davecramer)" <[email-masked]>
Date: Thu, 18 Dec 2025 13:07:15 +0000

fixes #3892

---

From: "CTH57 (@CTH57)" <[email-masked]>
Date: Mon, 05 Jan 2026 15:51:23 +0000

Are you sure of your correction ?
If the connection is read only, the attempt to execute the prepare transaction will throw an exception. 
The test conn.isReadOnly should be before.

---

From: "CTH57 (@CTH57)" <[email-masked]>
Date: Mon, 05 Jan 2026 15:53:57 +0000

Proposition 👍 

    if (conn.isReadOnly()) {
      return XA_RDONLY;
    }
    
    state = State.IDLE;
    preparedXid = currentXid;
    currentXid = null;

    try {
      String s = RecoveredXid.xidToString(xid);

      try (Statement stmt = conn.createStatement()) {
        stmt.executeUpdate("PREPARE TRANSACTION '" + s + "'");
      }
      conn.setAutoCommit(localAutoCommitMode);
      
      return XA_OK;
    } catch (SQLException ex) {
      throw new PGXAException(GT.tr("Error preparing transaction. prepare xid={0}", xid), ex, mapSQLStateToXAErrorCode(ex));
    }

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Mon, 05 Jan 2026 15:57:37 +0000

Is it not possible to have a XA transaction on read only connections ?

---

From: "CTH57 (@CTH57)" <[email-masked]>
Date: Tue, 06 Jan 2026 07:19:20 +0000

If during a transaction, a read only connextion is used to select data and another connection is used to insert data, boths connections are enlist in the XATransaction.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Tue, 06 Jan 2026 10:47:35 +0000

After doing more reading I'm not even sure we should be returning XA_RDONLY at all, this is the job of the XA manager. XA_RDONLY is returned by prepare when no data has changed, ie the transaction only reads data. As such no commit is necessary. see https://www.ibm.com/docs/en/i/7.6.0?topic=applications-example-using-cli-xa-transaction-connection-attributes or https://docs.oracle.com/javase/8/docs/api/javax/transaction/xa/XAResource.html#XA_RDONLY. If in fact you provide a read only connection and a writer connection and expect data to be written the correct thing to do here is throw an exception as the data was not written and can't possibly be written.

---

From: "CTH57 (@CTH57)" <[email-masked]>
Date: Tue, 06 Jan 2026 11:04:49 +0000

"XA_RDONLY is returned by prepare when no data has changed, ie the transaction only reads data"

Yes and it's obviously the case if the connection is read only.

In my case, the read only connection is used only to read data, so no need to commit this connection.