請教一個關於spring事務的問題 ?

zgli發表於2005-11-24

由於問題較長,希望各位高手能夠耐心看完,謝謝, 資料庫mysql.

CREATE TABLE `CUSTOMERS` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`sex` VARCHAR(45) NOT NULL,
`age` INTEGER NOT NULL,
PRIMARY KEY(`id`)
)
ENGINE = InnoDB;

所有jdbc實現的dao基類

public class DAO {
    DataSource dataSource;
	public void setDataSource(DataSource dataSource)
		throws DataAccessException {
		this.dataSource = dataSource;
	}
}


ICustomerDAO介面

public interface ICustomerDAO {	
	Long insertCustomer(Customer customer)throws DataAccessException;
}
<p class="indent">


ICustomerDAO介面的實現類CustomerDAO_Imp繼承DAO實現ICustomerDAO

public class CustomerDAO_Imp extends DAO implements ICustomerDAO {

	public Long insertCustomer(Customer customer) throws DataAccessException {
		Long result = null;
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			String sql = "INSERT INTO CUSTOMERS(name,sex,age)values(?,?,?);";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, customer.getName());
			pstmt.setString(2, customer.getSex());
			pstmt.setInt(3, customer.getAge());
			pstmt.execute();
			pstmt.close();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select max(id) from CUSTOMERS ;");
			rs.next();
			result = new Long(rs.getLong(1));
			rs.close();
			stmt.close();
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DataAccessException(e.getMessage());
		} finally {
			try {
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw new DataAccessException(e1.getMessage());
			}
		}
	}

服務介面

	public interface IService {
	void method1();
	}

IService服務介面實現類Service_Imp
public class Service_Imp implements IService {

ICustomerDAO customerDAO;
public void setCustomerDAO(ICustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}

public void method1() { //fail
Customer customer = new Customer();
customer.setName("Jim");
customer.setSex("Male");
customer.setAge(24);
Long cid = customerDAO.insertCustomer(customer);
Order order = new Order();

System.out.println(
"customers table should not contains customer with id " + cid);

int i = 1 / 0; //exception in service method

}
配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
       <property name="driverClassName">
           <value>org.gjt.mm.mysql.Driver</value>
       </property>
       <property name="url">
           <value>jdbc:mysql://localhost/test</value>
       </property>
       <property name="username"><value>root</value></property>
       <property name="password"><value>root</value></property>
    </bean> 
    
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource">
           <ref local="dataSource" />
       </property>
    </bean>
    
    <bean id="customerDAO" class="dao.imp.jdbc.CustomerDAO_Imp">
      <property name="dataSource">
        <ref local="dataSource" />
      </property>
    </bean>  
    
    <bean id="serviceImp"
          class="service.imp.Service_Imp">      
       <property name="customerDAO">
          <ref local="customerDAO" />
       </property>
    </bean>      
    
    <bean id="service"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
       <property name="transactionManager">
          <ref bean="transactionManager" />
       </property>
       <property name="target">
          <ref local="serviceImp" />
       </property>
       <property name="transactionAttributes">
          <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
          </props>
       </property>
    </bean>  
</beans>
<p class="indent">

測試過程

public class testTransaction {	
	public static void main(String[] args) {
		ApplicationContext context =
			new ClassPathXmlApplicationContext("bean.xml");
		IService service = (IService) context.getBean("service");
		int testMethod = 1;
		switch (testMethod) {
			case 1 :
				service.method1();
				break;
			case 2 :
			
<p class="indent">

輸出

customers table should  not contains customer with id 1
java.lang.ArithmeticException: / by zero
	at service.imp.Service_Imp.method1(Service_Imp.java:32)
<p class="indent">


問題:
在service.method1();中出現了/ by zero異常,這個方法在事務中
為什麼資料庫中有customer with id 1 沒有回滾 ??
我錯在哪裡了?請各位解釋一下spring管理事務過程好麼?

相關文章