Java Reference
In-Depth Information
Table 4-2. Sample Data in the BOOK Table for Testing Transactions
ISBN
BOOK_NAME
PRICE
0001
The First Topic
30
Table 4-3. Sample Data in the BOOK_STOCK Table for Testing Transactions
ISBN
STOCK
0001
10
Table 4-4. Sample Data in the ACCOUNT Table for Testing Transactions
USERNAME
BALANCE
user1
20
Then write the following Main class for purchasing the topic with ISBN 0001 by the user user1 .
Because that user's account has only $20, it is not sufficient to purchase the topic.
package com.apress.springenterpriserecipes.bookshop.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext( " beans.xml " );
BookShop bookShop = (BookShop) context.getBean( " bookShop " );
bookShop.purchase( " 0001 " , " user1 " );
}
}
When you run this application, you will encounter a SQLException because the CHECK constraint of
the ACCOUNT table has been violated. This is an expected result because you were trying to debit more
than the account balance. However, if you check the stock for this topic in the BOOK_STOCK table, you will
find that it was accidentally deducted by this unsuccessful operation! The reason is that you executed
the second SQL statement to deduct the stock before you got an exception in the third statement.
Search WWH ::




Custom Search