Repository navigation
[Server] Give Hibernate and Casbin one HikariCP pool - #1863
Conversation
| 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()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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]>
087ecba to
99289cd
Compare
PR Checklist
docsis updatedDescription 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:
HibernateConfiguratorfromhibernate.connection.*plus optionalhibernate.hikari.*keys.DataSourceto Hibernate (jakarta.nonJtaDataSource) and to JCasbin'sJDBCAdapter.rollback()(MySQL and PostgreSQL reject rollback on autocommit connections). Casbin's one checkout is forced to autocommit soloadPolicydoes not leave Postgres/MySQL idle-in-transaction.Username resolution still prefers
hibernate.connection.usernameand falls back tohibernate.connection.user.Test plan
HibernateConfiguratorTest— pool size/name/autocommit, username fallbackJCasbinAuthorizerTest— grant/revoke/hierarchy/list; Casbin checkout is autocommit, Hibernate pool is notJCasbinAuthorizerMultiInstanceTest— cross-replica refresh still worksUnityAccessEvaluatorTest— deny-path refreshTransactionManagerTest— isolation restore and REPEATABLE_READ snapshotserver/testlocally (498 passed, 2 skipped)server/testin CI