2

I am trying to find out tiny differences in Hibernate update queries and I have come to two interesting possibilities:

  1. Executing updates in transaction (for instance in methods annotated with @Transactional.
  2. Executing updates without transaction as follows:

`

Connection connection = entityManager.getDataSource().getConnection();
connection.prepareStatement(...);
affectedRows = ps.executeUpdate();

The second doesn't create transactions, so I guess it could be faster? Consider very simple update queries, for which I don't really need transaction. What is the real difference?

Vojtěch
  • 11,312
  • 31
  • 103
  • 173
  • 2
    the difference is the same as using hibernate vs using plain jdbc as that is what you are doing in the second case. Also there is always a transaction... That statement is probably in autocommit mode which will open and commit a transaction for every statement – Zeromus Mar 19 '18 at 08:25

1 Answers1

1

Hibernate is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Most importantly hibernate is quite faster in performance when there's a huge load.

Transaction is an interface that defines the unit of work It maintains abstraction from the transaction implementation (JTA,JDBC). A transaction is associated with Session. So to get maximum out of hibernate you need transaction. If not there's no point of using hibernate at all.

The main advantage of hibernate is that it can handle many requests at once and it's easy to use, but if you have less than 50 requests per minute don't use hibernate it's just a waste of RAM.

Read more,

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80