SELECT also requires a transaction. It is not possible to execute SELECT without any transaction. The fact that you do not need to explicitly start and end the transaction when selecting data from the database using some SQL GUI tools is that these tools use autocommit . In autocommit mode, the database will automatically start and commit the transaction for each individual SQL statement, so you do not need to explicitly declare the transaction boundary.
If you donβt finish the transaction (for example, commit or rollback), the database connection used by this transaction will not be released, and ultimately your database will be exhausted from the available connections.
For sleep mode, the default mode is non-autosave (which is specified by the hibernate.connection.autocommit property). Therefore, we must declare the border of the transaction and ensure that the transaction is complete.
But if you only request data using the hibernate API, this is fine even if you do not explicitly declare the transaction boundary due to the following events:
Typically, a database automatically starts a new transaction when the current SQL statement requires one, and the transaction does not explicitly start earlier.
Session.close() will close () the underlying Connection . JDBC specification, if java.sql.Connection # close () is called and an active transaction occurs, the result of this active transaction depends on the implementation of the JDBC provider. Typically, the JDBC driver automatically commits or rolls back this transaction. But in this case, it doesnβt matter if the transaction or rollbacks, because you already received the necessary data.
Ken chan
source share