Sivaranjani D
Sivaranjani D

Reputation: 532

Spring hibernate transaction

The documentation says we have to begin the transaction before doing some insertion. But how does the code below run? Because I am calling beginTransaction() after save.

    @Override
public void insertCustomer(Employees employee) {

    Session session = sessionFactory.openSession();
    session.save(employee);
    session.beginTransaction();
    session.getTransaction().commit();
    session.close();
}

And I have one more doubt on Spring's @transactional attribute called propagation which can be configured like below,

@Transactional(propagation=Propagation.NEVER)

And this one is said to be run non-transitionally. what does that mean?

Please help me to understand the above concepts

Upvotes: 1

Views: 864

Answers (1)

Kalyan
Kalyan

Reputation: 1939

How the below code runs? Because I am calling the begintransaction after save.

Hibernate will not try to insert/update the database as soon as you call save(). It will try to insert only when you try to commit a transaction. If you don't commit a transaction, it will not run as you expect.

@Transactional(propagation=Propagation.NEVER) what does that mean?

Following are the 7 different Transaction propagation behaviors supported by Spring

  • PROPAGATION_REQUIRED (default)
  • PROPAGATION_REQUIRED_NEW
  • PROPAGATION_SUPPORTS
  • PROPAGATION_NOT_SUPPORTED
  • PROPAGATION_MANDATORY
  • PROPAGATION_NEVER
  • PROPAGATION_NESTED

(Note: You don't need to use a transaction in all your methods. You can also have a normal method without a transaction)

To execute a method under a transaction, annotate it with @Transactional. By default it runs under PROPAGATION_REQUIRED behavior which means one of the two things

  1. New transaction will be activated if there is no existing transaction in the caller.
  2. Join the existing transaction if one is already activated by the caller.

When a method is annotated with @Transaction(prorogation=Propagation.NEVER) it shouldn't be called, by another method, with an active transaction. Otherwise, an exception will be thrown.

Propagation.NEVER is used when you want a specific logic/method to be executed strictly without an active transaction. In my experience, I have never used this attribute. 95% of the time you will be satisfied with the default PROPAGATION_REQUIRED behavior. However, it is good to have a knowledge on different behaviors.

Let me explain with an example:

@Component
public class TransactionTutorial {

    @Transactional
    public void aboutRequired() {
        //This method will create a new transaction if it is called with no active transaction 

        //Some logic that should be executed within a transaction

        //normal() method will also run under the newly created transaction.
        normal();

        //An exception will be thrown at this point
        //because aboutNever() is marked as Propagation.NEVER 
        aboutNever();
    }

    @Transactional(propagation=Propagation.NEVER)
    public void aboutNever() {
        //This method should be called without any active transaction.
        //If it is called under active transaction, exception will occur.

        //Some logic that should be executed without a transaction.
    }

    public void normal() {
        //This method is not bothered about surrounding transaction.
        //You can call this method with or without an active transaction.

        //Some logic
    }
}

Reading the above code might give better understanding.

Upvotes: 2

Related Questions