日期:2014-05-20  浏览次数:20833 次

spring事务怎么用
使用spring事务的时候遇到一个莫名其妙的问题
Java code


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test1 {

    public void test() {
        System.out.println("test1");
        ApplicationContext ctx = 
                new ClassPathXmlApplicationContext("bean.xml");//到这里就不能继续运行了
        System.out.println(ctx);
        Transaction ts = (Transaction)ctx.getBean("tsa");
        System.out.println("test");
        ts.insert("insert into user (id,name,password,age) value(10,'sadasd','12312313',11)");        
    }

}


输出结果是:test1
这是什么原因,bean.xml在src目录中
bean.xml
XML code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost/javaee"></property>
    <property name="user" value="root"></property>
    <property name="password" value="root"></property>
    <property name="maxPoolSize" value="40"></property>
    <property name="minPoolSize" value="1"></property>
    <property name="initialPoolSize" value="1"></property>
    <property name="maxIdleTime" value="20"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="tsa" class="com.daisy.dao.Transaction">
    <property name="ds" value="dataSource"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut expression="* com.daisy.dao.Transaction.*(..)" id="daoCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="daoCut"/>
</aop:config>
</beans>





Transaction.java
Java code
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;


public class Transaction {
    private DataSource ds;

    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
    }
    public void insert(String sql){
        JdbcTemplate jt = new JdbcTemplate(ds);
        jt.execute(sql);
        System.out.println(sql);
        //jt.execute(sql);
    }
}