Sachin Singh
Sachin Singh

Reputation: 39

Transaction is optional in Hibernate . But why do we need to use it every time? Is there any alternative?

I found that using transaction is optional as hibernate application manages transaction in their own application code.But when i try to execute the code without Transaction,the code executes without any error but there is no data in the database. How can i commit my data without using Transaction(since it is optional)? What is the alternative of Transaction?

 Configuration c = new Configuration();
 c.configure("hibernate.cfg.xml");
 SessionFactory sf = c.buildSessionFactory();

 Session s = sf.openSession();

 //Transaction t = s.beginTransaction();
 //some code
 //t.commit();

Upvotes: 0

Views: 883

Answers (2)

Ivan
Ivan

Reputation: 3836

Database, or system, transaction boundaries are always necessary. No communication with the database can occur outside of a database transaction (this seems to confuse many developers who are used to the auto-commit mode). Always use clear transaction boundaries, even for read-only operations (c) Hibernate transaction demarcation doc

There are docs saying that Transaction is optional.

(Optional) A single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC, JTA or CORBA transaction. A org.hibernate.Session might span several org.hibernate.Transactions in some cases. However, transaction demarcation, either using the underlying API or org.hibernate.Transaction, is never optional. Basic API's / architecture doc

What that actually means is that if you run your code in an environment (container / framework) which does transaction demarcation for you - you can skip using Hibernate API's for this.

Alternatives to this API's are Spring Declarative Transaction management (basically annotations, they have XML way too), Transaction demarcation in an EJB container, etc - those depend on where you run your code.

Transaction is basic work unit of a database and they are needed for working the DB's, somebody working with a DB should always specify where a transaction starts / ends.

You can read more on ACID guarantees for some context.

Upvotes: 2

bananas
bananas

Reputation: 1196

from docs

public interface 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 instantiated by a call to 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.

and therefore it is clear that you cannot do any transaction without Transaction

Upvotes: 1

Related Questions