Reputation: 1792
I'm using hibernate 5.2 for connection to SQLite
library. I created a new Session
and used session.createNativeQuery("My sql").executeUpdate()
after what I closed my Session
. Everything works fine, but I came across several examples where after creating Session
they begin Transaction
, perform SQL
operations, commit Transaction
, and close Session
. But not all examples online have Transaction
in them and my code works fine without it.
This made me curious:
Transaction
?I use <property name="hibernate.connection.pool_size">1</property>
Upvotes: 2
Views: 1393
Reputation: 5232
Let's define what is transaction - basically it is atomic unit of work.
There are two types of transaction management or transaction demarcation(think of demarcation as of starting transaction, committing or rolling transaction back) : CMT (container managed transactions) - managed by underlying container for you (JTA) and BMT(bean managed transactions) - transaction demarcation is managed programmatically by developer itself. So if you see in any example that transaction
is obtained and programmatically committed or rolled back - that is example of BMT, that is when it is your responsibility of a developer to manage transaction.
Whenever you do not see explicit transaction demarcation - that means that is is CMT. This is very broad subject - I advise you to read more.
Upvotes: 0
Reputation: 702
You can try relating hibernate with JDBC and you will be getting some hints about transaction.
In JDBC you need to just open a connection start your work, and in the end you can commit or rollback.
But what if you have many different parallel tasks , which may be dependent or independent to each other. Then you may require commit/rollback each task separately or rollback if any fails.
for example
Big Task :
small task1
small task2
small task3 and many more
rollback the big task if any small task fails.This can be one of the many business requirements.
In JDBC, Connection interface has provided commit() and rollback() methods.
In jpa/hibernate, Transaction interface has provided commit() and rollback() methods.
So one session can have many dependent or independent transactions.
Below is documentation from org.hibernate.Transaction
Allows the application to define units of work, while maintaining
abstraction from the underlying transaction implementation (eg. JTA, JDBC).
A transaction is associated with a Session and is usually
initiated by a call to org.hibernate.Session.beginTransaction().
A single session might span multiple transactions since the notion of a session
(a conversation between the application and the datastore) is of coarser granularity
than the notion of a transaction. However, it is intended that there be at most
one uncommitted transaction associated with a particular Session at any time.
This also might help you What is the difference between a session and a transaction in JPA 2.0?
Upvotes: 0
Reputation: 414
Why we need to use Transaction? -> What you are doing is just a simple update, transaction mgmt is used in case there are multiple updates and you want ACID nature for your transaction.
What will happen if we won't? -> If there are multiple update statements and one of them throws an exception you will have inconsistent data.
Upvotes: 1