Steeps fpr Spring Web Application Transaction
-------------------------------------------------------------
Copy the jar files those are supplied by the spring people to D:\sprlibs directory.
Copy commons-dbcp-1.2.2.jar, commons-pool-1.3.jar, commons-collections-3.2.jar and ojdbc14.jar are to the same directory.
Remove hibernate2.jar and spring-hibernate2.jar files instead of using the spring jar file supplied by eclipse people used by provided by spring people.
Create table bacc(aid number(10) Primary key, balance number(10,2));
Insert into back values (1,10000);
Insert into back values (2,20000);
MyEclipse IDE will able to generate the DAO class can used by spring application.
For back table MyEclipse generates –
pack.Bacc (POJO class), back.hbm.xml (mapping file), pack.BaccDAO (DAO class).
Transaction Service Code –
---------------------------------------------
package pack;
public class FTManager {
private BaccDAO dao;
public void setDAO(BaccDAO dao) {
this.dao=dao;
}
public void trasferFunds(Long aidOne, Long aidTwo, double amt) throws Exception {
Bacc b1,b2;
b1=dao.findById(aidOne);
b2=dao.findById(aidTwo);
b1.setBalance(new Double(b1.getBalance().doubleValue()-amt));
b1.setBalance(new Double(b1.getBalance().doubleValue()+amt));
}
}
Provide Main application Classes -
---------------------------------------------
package pack;
public class TxManager {
public static void main (Sting [] args) throws Exception {
ApplicationContext sc = new ClassPathXmlApplicationContext (“applicationContext.xml”);
Object obj;
obj = sc.getBean(“fmt”);
FTManager ftm=(FTManager) obj;
ftm.transaferFunds(new Long(1),new Long(2),1000);
}
}
Provide the info about the Service classes in applicationContext.xml file -
------------------------------------------------------------------------------
< id="”txManager”" class="”org.springframework.orm.hibernate3.HibernateTransactionManager”">
< name="”sessionFactory”">
< local="”sfBean”">< /ref>
< /property>
< /bean>
< id="”baccDAO”" class="”pack.BaccDAO”">
< name="”sessionFactory”">
< local="”sfBean”">< /ref>
< /property>
< /bean>
< id="”ftmTarget”" class="”pack.FTManage”">
< name="”dao”">
< local="”baccDAO”">< /ref>
< /property>
< /bean>
< id="”fmt”" class="”org.springframework.transaction.intercepter.TransactionProxyFactoryBean”">
< name="”transactionManager”">
< local="”txManager”">< /ref>
< /property>
< name="”target”">
< local="”fmtTarget”">< /ref>
< /property>
< name="”transactionAttributes”">
<>
< key="”*”" value="”PROPAGATION_REQUIRES_NEW,">
< /entry>
< /map>
< /property>
< /bean>
o Here ‘*’ represents all the methods.
o The Transaction Manager will start the new transaction before executing the business method.
o Transaction Manager roleback the transaction when exception will be thrown by the business method.
Note:
For configuring multiple business service classes we can use the configuration info provided in documentation of TransactionProxyFactoryBean.
As part of a spring based application we can provide the code to manage the transaction but this is not a recommended approach. We can use declarative transaction management by providing/ declaring the info about how the transaction has to be managed using transactionAttributes.
By default spring will roleback the transaction if an exception of type java.lang.Exception of its subclasses is thrown.
If we provided +java.lang.Exception as part of transactionAttributes the transaction will be committed when the exception will be thrown.
< key="”bmOne”" value="”PROPAGATION_REUIRED_NEW,">< /entry>
o PROPAGATION_REUIRED_NEW
o PROPAGATION_REUIRED (default)
o PROPAGATION_MANDATORY
o PROPAGATION_SUPPORTED
o PROPAGATION_NOT_SUPPORTED
o PROPAGATION_NEVER
• PROPAGATION_REUIRED_NEW
-----------------------------------
Spring will start a new transaction before executing the method. The transaction will be started even if there is an transaction.
• PROPAGATION_REUIRED
-----------------------------------
If we use PROPAGATION_REQUIRED spring will start a new transaction if there is no transaction. If there is a transaction, if we use PROPAGATION_REQUIRED then the method will be executed as part of existing transaction.
• PROPAGATION_MANDATORY
-----------------------------------
If there is no transaction then it is considered to be illegal. If there is a transaction then method will be executed as part of the existing transaction.
• PROPAGATION_SUPPORTED
-----------------------------------
If there is no transaction then method will be executed without transaction. If there is a transaction method will be executed as part of existing transaction.
• PROPAGATION_NOT_SUPPORTED
-----------------------------------
If there is no transaction then method will be executed without transaction. If there is a transaction method will be executed without transaction.
• PROPAGATION_NEVER
-----------------------------------
If there is no transaction then method will be executed without transaction. If there is a transaction will be not executed and this is considered to be illegal.
Note:
It is recommended to use the above three values when we want to perform the DB operation from a business method.
No comments:
Post a Comment