Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support use of JTA transaction ? #54

Open
scalajos opened this issue May 29, 2022 · 5 comments
Open

Support use of JTA transaction ? #54

scalajos opened this issue May 29, 2022 · 5 comments

Comments

@scalajos
Copy link

Support use of JTA / XA transaction

JTA transactions have some strict threading constraint :
to be part of a JTA transaction, a jdbc action must execute within a thread already bound to a JTA transaction
More specifically:

  • obtaining a connection from an xa datasource needs to be done with a thread already bound to a JTA transaction to enroll the jdbc connection (as a XAResource) into the transaction manager
  • executing a jdbc statement or any action using a jdbc connection needs to be done with a thread already bound to a JTA transaction

Using blocking IO pool (eg blocking {...}) naively would prevent any use of the library in a JTA context (no way to set XA transaction in thread context of the blocking section)

Here 2 quick ideas I propose but you certainly will find much better ones

  1. allow some scoped action in the 'blocking' block to have a chance to access thread and set/unset a JTA context for instance
val scopedEnterExitBlockingSectionAction: ZIO[Scope, ..., Unit] = ... // obtain the 'blocking' customizer

blocking {
  ZIO.scoped {
    for {
      // a scoped zio action (possibly no-op) which could access current thread and set thread context
      // for inst. bind/unbind the current thread with a JTA transaction 
      _ <- scopedEnterExitBlockingJdbcSectionAction 
      // get connection from XA datasource or use some jdbc action
      r <- jdbc action
    } yield r
  }
}
  1. allow external custom blocking IO executor
val jdbcBlockIo = ZIO.attempt {
  jdbc action 
}

if (customExecutorDefined) jdbcBlockIo.onExecutor(customExecutor)
else blocking(jdbcBlockIo)

where customExecutor would provide a jdbc IO executor which thread is guaranteed to be bound to a JTA transaction

I understand these are very rough ideas but I really think supporting JTA/XA transactions would be a significant advantage for the library

Best regards

@jdegoes
Copy link
Member

jdegoes commented May 17, 2023

@scalajos Sounds good to me!

/bounty $100

@algora-pbc
Copy link

algora-pbc bot commented May 17, 2023

💎 $100 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #54 with your implementation plan
  2. Submit work: Create a pull request including /claim #54 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-jdbc!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🔴 @cacapouh Jun 8, 2023, 8:32:04 AM WIP

@cacapouh
Copy link

cacapouh commented Jun 8, 2023

/attempt #54

@algora-pbc
Copy link

algora-pbc bot commented Jun 15, 2023

@cacapouh: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏

@algora-pbc
Copy link

algora-pbc bot commented Jun 22, 2023

The bounty is up for grabs! Everyone is welcome to /attempt #54 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants