-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DAO org.springframework.jdbc.datasource.DataSourceTransactionManager
- Loading branch information
1 parent
b02550d
commit 2b6343f
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/resources/java/com/wwq/bookshop/TransactionalJdbcBookShop.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters