Transactions in Spring and EJB Context.
I will go to straight point to how Transactions useful ,instead of explaining all ACID Properties .In this first part we will discuss about types of transactions.
There are 3 types of transaction u see in the java world.
1) Local Transactions
2) Programatic Transactions.
3) Declarative Transactions.
1) Local Transactions :
In Local Transactions ,u no need to worry about the transactions ,underlying managers take care ,for example if u take JMS related transactions there is QueueConnectionFactory which is the resource manager take care, if u take jdbc related things it will take care of default commit, u no need to worry about the things.
But say
Connection con=datsource.getConnection();
Statement st=con.createStatement();
String sql=”some statement”;
String sql1=”some statement”;
Try {
St.executeUpdate(sql);
St.executeUpdate(sql1);
Con.commit();
}catch(Exception e)
{
Con.rollback();
}
Here Defaulty transaction Manager will commit 2 things ,as we didn’t set autoCommit false.Say for example. First executeUpdate executes fine then any problem occurs,the rollback will happen to only upto 2nd statement.First execute Update statement still commited. So inorder to maintain Local Transactions , we will use
Con.setAutoCommit(false);
So Even one execute Statement fails automatically we can roll back all the statements.
2) Programatic Transactions :
Here in this Developer manages the transactions not like local transactions giving control to resource mangers to handle the transaction. Here the Comes for JTA(Java Transaction API).There one interface named UserTransaction Comes to picture.
Say in EJB. U will get the SessionContext or messsageDrivenContext what ever
UserTransaction ut= sessionContext.getUserTransaction;
ut.begin();
//write code
Ut.commit();
Where ever u want which individual statements want to commit or roll back u can decide and write the code.
3)Container Managed Transactions:
Here The developer don’t want to write code as begin,commit ,rollback Every thing container will take care ,just developer can say which method to do transaction and behaviour of transactions.
In EJB we can declare it in ejb-jar.xml file.
There are 6 attributes like never,Mnadatorey,Required etc given by EJB Container.Ofcourse In Spring Propagation_nested attribute extra added for chaining.
We will discuss these transactions in depth and even how it will help developers where to use each type of transactions and drawbacks of all these.Here for time being iam not discussing about spring Transactions.In next session we will discuss spring related transactions even too. Have a good Day !!!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment