Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need of Transaction API in Java

First of all my question is What is the need of Transaction API in java ? Give me the practical example?

What is the meaning for Container Managed Transaction and Bean Managed Transaction?

And Difference between Declarative Transaction and Programmatic Transaction?

Please help me

Thanks in advance

like image 727
Dilip Ganesh Avatar asked Mar 19 '26 05:03

Dilip Ganesh


1 Answers

Declarative transaction: you put the transaction declarative in the method declaration. so you doesn't need to implement the transaction manually. Here I give you the example:

// declarative
@Transcational
public void Transfer (Account from, Account destination, double amount) {
//do your logic here
}

// programmatic
public void Transfer (Account from, Account destination, double amount) {
    var session = sessionFactory.openSession();
    var tx = session.BeginTransaction();

    try {
        //do you logic here
        tx.Commit();
    } catch {
        tx.Rolback();
    }
}
like image 200
Adi Sembiring Avatar answered Mar 21 '26 19:03

Adi Sembiring