package sinha.priyank.txn;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// How we are injecting JdbcTemplet in this class as we dont have //any getter/setter and constructor
@Transactional
public class AccountService extends SimpleJdbcDaoSupport {
@Transactional(propagation=Propagation.REQUIRED,timeout=10)
public void deposit(int accountNumber , double amount)
{
getSimpleJdbcTemplate().update("update accounts set balance=balance+? where account_number=?", amount,accountNumber);
getSimpleJdbcTemplate().update("insert into statements (account_number,transaction_amount,transaction_type,id) values(?,?,?,?)", accountNumber , amount , "Deposit",121);
}
@Transactional(propagation=Propagation.REQUIRES_NEW , timeout=40)
public void withdraw(int accountNumber , double amount)
{
getSimpleJdbcTemplate().update("update accounts set balance=balance+? where account_number=?", amount,accountNumber);
getSimpleJdbcTemplate().update("insert into statements (account_number,transaction_amount,transaction_type,id) values(?,?,?,?)", accountNumber , amount , "Withdraw",180);
}
}
/* * // SPring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:tx="http://ift.tt/OGfeU2"
xmlns:aop="http://ift.tt/OpNdV1"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/QEDs1e
http://ift.tt/OGfeU2
http://ift.tt/1cQrvTl
http://ift.tt/OpNdV1
http://ift.tt/QEDs1g">
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521/xe"/>
<property name="password" value="khanjan"/>
<property name="username" value="system"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceBean"/>
</bean>
<bean id="jdbcTemplateBean" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceBean"/>
</bean>
<bean id="accountServiceBean" class="sinha.priyank.txn.AccountService">
<property name="jdbcTemplate" ref="jdbcTemplateBean"/>
</bean>
</beans>
* */
/********* // Main Program
package sinha.priyank.txn;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TxnClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// This is a class contain void main
AccountService accountservices = context.getBean("accountServiceBean" , AccountService.class);
System.out.println("***** Calling Deposit ****");
accountservices.deposit(1001, 500);
System.out.println("***** Calling Withdraw *****");
accountservices.withdraw(1001, 300);
System.out.println("ACCOUNT OPERATION COMPLETED SUCCESSFULLY");
}
}
*****/
Aucun commentaire:
Enregistrer un commentaire