Skip to content

Commit

Permalink
TransactionManager
Browse files Browse the repository at this point in the history
DAO
org.springframework.jdbc.datasource.DataSourceTransactionManager
  • Loading branch information
wangweiqing committed Aug 1, 2014
1 parent b02550d commit 2b6343f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/resources/java/com/wwq/bookshop/JdbcBookShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void purchase(String isbn, String username) {
Connection conn = null;
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false);

PreparedStatement stmt1 = conn
.prepareStatement("SELECT PRICE FROM BOOK WHERE ISBN = ?");
Expand All @@ -44,7 +45,15 @@ public void purchase(String isbn, String username) {
stmt3.setString(2, username);
stmt3.executeUpdate();
stmt3.close();

conn.commit();
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
}
}
throw new RuntimeException(e);
} finally {
if (conn != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.wwq.bookshop;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class TransactionalJdbcBookShop extends JdbcDaoSupport implements
BookShop {

private PlatformTransactionManager transactionManager;

public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}

@Override
public void purchase(String isbn, String username) {
// TODO Auto-generated method stub
TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction(def);
try {
int price = getJdbcTemplate().queryForInt(
"SELECT PRICE FROM BOOK WHERE ISBN = ?",
new Object[] { isbn });
getJdbcTemplate().update(
"UPDATE BOOK_STOCK SET STOCK = STOCK -1 "
+ "WHERE ISBN = ?",
new Object[] { isbn });
getJdbcTemplate().update(
"UPDATE ACCOUNT SET BALANCE = BALANCE - ? "
+ "WHERE USERNAME = ?",
new Object[] { price, username });
transactionManager.commit(status);
} catch (DataAccessException e) {
transactionManager.rollback(status);
throw e;
}

}

}
7 changes: 6 additions & 1 deletion src/main/webapp/WEB-INF/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
<property name="username" value="weiqingwang"/>
<property name="password" value="wwqwwq"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="bookShop"
class="com.wwq.bookshop.JdbcBookShop">
class="com.wwq.bookshop.TransactionalJdbcBookShop">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager"/>
</bean>


Expand Down

0 comments on commit 2b6343f

Please sign in to comment.