Skip to content

[Server] Give Hibernate and Casbin one HikariCP pool - #1863

Merged
yili-db merged 4 commits into
unitycatalog:mainfrom
s-maling-celonis:fix/shared-hikari-pool-upstream
Sep 25, 2026
Merged

yili-db merged 4 commits into
unitycatalog:mainfrom
s-maling-celonis:fix/shared-hikari-pool-upstream

Conversation

@s-maling-celonis

@s-maling-celonis s-maling-celonis commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description of changes

Hibernate currently uses its built-in non-production JDBC pool, and JCasbin opens a separate JDBC connection for policy storage. Those two paths have no shared bound, so total database connections are Hibernate's pool plus Casbin's extra connection.

This change puts both under one HikariCP pool so the cap is hibernate.hikari.maximumPoolSize (not Hibernate pool + 1). jdbc-adapter 2.7.0 still checks out one connection in its constructor and holds it for the authorizer lifetime, so this does not share connections with Hibernate and does not relieve reload-vs-catalog contention. Real sharing would need a per-operation-checkout adapter.

In detail:

  • Adds HikariCP 7.1.0 and builds one pool in HibernateConfigurator from hibernate.connection.* plus optional hibernate.hikari.* keys.
  • Hands that DataSource to Hibernate (jakarta.nonJtaDataSource) and to JCasbin's JDBCAdapter.
  • Defaults pool autocommit to false so Hibernate can rollback() (MySQL and PostgreSQL reject rollback on autocommit connections). Casbin's one checkout is forced to autocommit so loadPolicy does not leave Postgres/MySQL idle-in-transaction.
  • Closes the pool with the session factory, and closes the Casbin JDBC adapter with the authorizer.

Username resolution still prefers hibernate.connection.username and falls back to hibernate.connection.user.

Test plan

  • HibernateConfiguratorTest — pool size/name/autocommit, username fallback
  • JCasbinAuthorizerTest — grant/revoke/hierarchy/list; Casbin checkout is autocommit, Hibernate pool is not
  • JCasbinAuthorizerMultiInstanceTest — cross-replica refresh still works
  • UnityAccessEvaluatorTest — deny-path refresh
  • TransactionManagerTest — isolation restore and REPEATABLE_READ snapshot
  • server/test locally (498 passed, 2 skipped)
  • server/test in CI

String user = resolveConnectionUsername(properties);
String password = properties.getProperty("hibernate.connection.password");
this.adapter = new JDBCAdapter(driver, url, user, password);
this.adapter = new JDBCAdapter(hibernateConfigurator.getDataSource());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jdbc-adapter 2.7.0 grabs one connection in its constructor and holds it for the authorizer's whole lifetime, never returning it to the pool between operations. Given that, what does routing Casbin through the shared pool actually buy?
It doesn't share connections (Casbin monopolizes one; Hibernate can never use it) and it doesn't relieve reload-vs-catalog contention (reloads already ran on Casbin's own connection). The only real gain is a single unified cap on total DB connections instead of "Hibernate pool + 1".

And it adds a cost: the pool defaults autoCommit to false, but the adapter's loadPolicy reads never commit, so on Postgres/MySQL Casbin's held connection sits idle-in-transaction until the first write (permanently on a refresh-only follower). CI is H2-only, so this won't surface there. If the goal is just a unified connection bound, can we say that plainly in the description? Actual sharing would need a per-operation-checkout adapter, which this one isn't.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right: jdbc-adapter 2.7.0 still takes one connection in the constructor and holds it, so this is a unified bound (maximumPoolSize), not connection sharing and not a fix for reload-vs-catalog contention. I updated the PR description and docs to say that. Per-operation checkout is not in any published adapter (including 2.13.0); that would be a follow-up.

The idle-in-transaction cost is real. The pool stays autoCommit=false for Hibernate's rollback(). Casbin's one getConnection() is forced to autocommit so loadPolicy does not leave a Postgres/MySQL transaction open on a refresh-only replica. Rather than adding an adapter, I opted for the simpler solution, adding a proxy around the pool.

@s-maling-celonis s-maling-celonis changed the title [Server] Give Hibernate and Casbin a shared HikariCP pool [Server] Give Hibernate and Casbin one HikariCP pool Sep 11, 2026
s-maling-celonis and others added 4 commits September 22, 2026 21:42
Hibernate used its built-in non-production pool and JCasbin opened its own JDBC connections, so policy reloads and catalog work could not share or bound database connections.

Signed-off-by: s-maling-celonis <[email protected]>
Co-authored-by: Cursor <[email protected]>
Operators can tune the pool with hibernate.hikari.* keys in hibernate.properties.

Signed-off-by: s-maling-celonis <[email protected]>
Co-authored-by: Cursor <[email protected]>
Match the existing copy-paste connection examples so operators can set pool size next to driver, URL, and credentials.

Signed-off-by: s-maling-celonis <[email protected]>
Co-authored-by: Cursor <[email protected]>
jdbc-adapter 2.7.0 holds one connection for the authorizer lifetime; leaving it on the Hibernate pool's autocommit-off default would sit idle-in-transaction after loadPolicy.

Co-authored-by: Cursor <[email protected]>

Signed-off-by: s-maling-celonis <[email protected]>
@s-maling-celonis
s-maling-celonis force-pushed the fix/shared-hikari-pool-upstream branch from 087ecba to 99289cd Compare September 22, 2026 19:45

@tdas tdas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build.sbt change approved

@yili-db
yili-db merged commit 1a7a2f3 into unitycatalog:main Sep 25, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants