-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
doc about using Session with "no transaction" #10223
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -233,6 +233,27 @@ entityManager.getTransaction().setRollbackOnly(); | |||||||||
|
|
||||||||||
| A transaction in rollback-only mode will be rolled back when it completes. | ||||||||||
|
|
||||||||||
| [[outside-transaction]] | ||||||||||
| .💀 Using the session with no transaction 💀 | ||||||||||
| **** | ||||||||||
| 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. | ||||||||||
| 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. | ||||||||||
|
Comment on lines
+239
to
+240
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| With that said, Hibernate grudgingly allows it. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| If you decide to do this weird thing: | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| - first, satisfy yourself that you're completely comfortable with the fact that _the database will not help you enforce transaction isolation_, | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| - then set link:{doc-javadoc-url}org/hibernate/cfg/JdbcSettings.html#AUTOCOMMIT[`hibernate.connection.autocommit=true`], and | ||||||||||
| - make damn sure that your connection pool is returning connections with autocommit enabled by default. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| 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. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm really not sure what kind of verifications you're asking for here. |
||||||||||
|
|
||||||||||
| 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>>. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure people know what they want better than us. They might not know what they need, though.
Suggested change
|
||||||||||
| And don't come complaining to us when your database starts filling up with inconsistent garbage. | ||||||||||
| // Because you've now been thoroughly warned, and we're going to enjoy laughing at you. | ||||||||||
|
Comment on lines
+253
to
+254
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| **** | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If replacing with an admonition:
Suggested change
|
||||||||||
|
|
||||||||||
| [[persistence-operations]] | ||||||||||
| === Operations on the persistence context | ||||||||||
|
|
||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well use admonitions instead of emojis?