67,513
社区成员
发帖
与我相关
我的任务
分享
<context:component-scan base-package="com.atom.bean.tx"></context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void updateStore(Integer bookId) {
String sql = "UPDATE book SET store = store-1 where bookid=?";
jdbcTemplate.update(sql, bookId);
}
}
@Repository
public class UserAccountDaoImpl implements UserAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void updateAccount(String userName , Integer account) {
String sql = "UPDATE useraccount SET account = account - ? where username = ?";
jdbcTemplate.update(sql, account , userName);
}
}
@Service
public class BuyServiceImpl implements BuyService {
@Transactional
public void buy(String username, Integer bookid) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-tx.xml");
BookDao dao = ctx.getBean(BookDao.class);
int store = dao.findStore(bookid);
if(store <= 0)
throw new RuntimeException("库存不足");
dao.updateStore(bookid);
UserAccountDao accountDao = ctx.getBean(UserAccountDao.class);
Integer accountInteger = accountDao.findAccount(username);
if(accountInteger < 100)
throw new RuntimeException("余额不足");
accountDao.updateAccount(username, 100);
}
}
public void testBuyService(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-tx.xml");
BuyService service = ctx.getBean(BuyService.class);
System.out.println(AopUtils.isAopProxy(service));
service.buy("aa", 1);
}
2016-02-16 22:32:41,806 INFO [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 2, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge14h9e1t2bvdv6fl1nz|6ef888f6, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge14h9e1t2bvdv6fl1nz|6ef888f6, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql:///test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 20, maxStatementsPerConnection -> 5, minPoolSize -> 5, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
2016-02-16 22:32:42,247 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6f45df59: startup date [Tue Feb 16 22:32:42 CST 2016]; root of context hierarchy
2016-02-16 22:32:42,248 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [bean-tx.xml]
2016-02-16 22:32:42,365 INFO [org.springframework.context.support.PropertySourcesPlaceholderConfigurer] - Loading properties file from class path resource [db.properties]
2016-02-16 22:32:42,502 INFO [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 2, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge14h9e1t2bvdv6fl1nz|757acd7b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge14h9e1t2bvdv6fl1nz|757acd7b, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql:///test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 20, maxStatementsPerConnection -> 5, minPoolSize -> 5, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]