Skip to content

Commit 8ab3b0e

Browse files
committed
stuff about using Session with "no transaction"
1 parent fec167b commit 8ab3b0e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

documentation/src/main/asciidoc/introduction/Interacting.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,27 @@ entityManager.getTransaction().setRollbackOnly();
233233

234234
A transaction in rollback-only mode will be rolled back when it completes.
235235

236+
[[outside-transaction]]
237+
.💀 Using the session with no transaction 💀
238+
****
239+
Over the years, we've run into a surprisingly large number of people who insist that they need to access the database with "no transaction" -- which we understand to mean running queries with https://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html#setAutoCommit--[JDBC autocommit mode] enabled.
240+
We're _pretty sure_ that this is almost always a terrible idea, and that most of the people doing this are simply mistaken about some aspect of transaction isolation or of what the use of autocommit implies for the management of pooled JDBC connections.
241+
242+
With that said, Hibernate grudgingly allows it.
243+
244+
If you decide to do this weird thing:
245+
246+
- first, satisfy yourself that you're completely comfortable with the fact that _the database will not help you enforce transaction isolation_,
247+
- then set link:{doc-javadoc-url}org/hibernate/cfg/JdbcSettings.html#AUTOCOMMIT[`hibernate.connection.autocommit=true`], and
248+
- make damn sure that your connection pool is returning connections with autocommit enabled by default.
249+
250+
In fact, simply don't do this unless you have set up tools to snoop the protocol-level interaction between the JDBC driver and the database server, so that you can verify your understanding of what "no transaction" actually implies.
251+
252+
If you think you want to actually _write_ to the database️ with autocommit enabled 🏴‍☠️🏴‍☠️🏴‍☠️,️ that's possible too, but you'll need to either <<flush,`flush()`>> the session explicitly or, even better, use a <<stateless-sessions,stateless session>>.
253+
And don't come complaining to us when your database starts filling up with inconsistent garbage.
254+
// Because you've now been thoroughly warned, and we're going to enjoy laughing at you.
255+
****
256+
236257
[[persistence-operations]]
237258
=== Operations on the persistence context
238259

0 commit comments

Comments
 (0)