日期:2014-05-16  浏览次数:20500 次

spring3之JdbcTemplate AOP 事务

一、applicationContext.xml配置:定义事务和aop

?

<!-- 定义事务传播属性   -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true"/>
            <tx:method name="*"  />
        </tx:attributes>
    </tx:advice>
      <tx:annotation-driven transaction-manager="txManager"/>
<!--aop错误引入spring.jar-->
    <aop:config>
        <!-- 描述切入点 -->
        <!--
1)execution(* *(..))
表示匹配所有方法
2)execution(public * com. savage.service.UserService.*(..))
表示匹配com.savage.server.UserService中所有的公有方法
3)execution(* com.savage.server..*.*(..))
表示匹配com.savage.server包及其子包下的所有方法
-->
        <aop:pointcut id="transactionPointCut" expression="execution(* com.cn.ssm.dao2..*.*(..))"/>
        <aop:advisor pointcut-ref="transactionPointCut" advice-ref="txAdvice"/>
    </aop:config>

?

二、实现类UserDaoImpl.java

@Transactional

public class UserDaoImpl implements UserDao {

    @Transactional(rollbackFor = Exception.class)
    public void insertUser() throws Exception {
        jdbcTemplate.update("insert into user (name) values ('01');");
        jdbcTemplate.update("update user set name=a where id=0;");
    }
}

?

三、Test.java 测试

?

?

 public static void main(String[] args) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_1.xml");
        UserDao userDaoImpl = (UserDao) context.getBean("userDaoImpl");
        userDaoImpl.insertUser();
     }

?

测试结果:

init:

deps-module-jar:

deps-ear-jar:

deps-jar:

compile-single:

run-main:

2011-11-26 1:22:38 org.springframework.context.support.AbstractApplicationContext prepareRefresh

信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1608e05: startup date [Sat Nov 26 01:22:38 CST 2011]; root of context hierarchy

2011-11-26 1:22:38 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [applicationContext_1.xml]

2011-11-26 1:22:38 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@107ebe1: defining beans [dataSource,txManager,txAdvice,org.springframework.aop.config.internalAutoProxyCreator,transactionPointCut,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,jdbcTemplate,userDaoImpl]; root of factory hierarchy

2011-11-26 1:22:39 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]

2011-11-26 1:22:39 org.springframework.jdbc.support.SQLErrorCodesFactory <init>

信息: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]

Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [update user set name=a where id=0;]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'a' in 'field list'

? ? ? ? at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:233)

? ? ? ? at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)

? ? ? ? at org.springframework.j