閱讀799 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Spring進行TestNG測試中無法插入、刪除數據庫數據(access)的解決

我的Spring項目在Eclipse上使用TestNG進行DAO的測試,也就是增刪改查的過程中,發現可以正常從access數據庫查詢數據,但是無法正常插入和刪除數據。console中沒有報錯,直接進入數據庫看,確實沒有修改的痕跡。可以正常查詢說明JdbcTemplate的操作本身沒有問題,但是不能修改數據庫。
這是為什麼?
遇到這個問題說明自己基礎知識差。
我當時的思路是這樣的:
可以查詢但是不能刪除和新增,說明我的sql語句沒有提交(commit)。
這種思路源於直接使用java語言特性編寫JDBC代碼的經驗,因為你需要在POJO中手動顯式的調用下麵的語句

setAutoCommit(false);
commit();

我當時的想法是,我的sql語句沒有被手動提交或者自動提交,因此沒有生效
這個想法是愚蠢的,因為,你並沒有直接寫POJO,你使用的是Spring提供的JdbcTemplate,你根本不用擔心“提交”這個問題,因為Spring封裝的JdbcTemplate讓這個問題對你透明了。

然後我覺得,一定和“事務”有關。
很可能是我寫的代碼,使用了Spring的“事務”,但是事務本身沒有被提交。
這樣想,就接近了問題的起因了。

我們捋順一下:
1.access數據庫本身是不存在“事務”支持的(不同於Mysql的Innodb引擎)
2.Spring代碼的事務支持

我查看自己的Spring配置文件,確實添加了事務管理器,如下:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="https://www.springframework.org/schema/beans"  
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"  xmlns:p="https://www.springframework.org/schema/p"
    xmlns:context="https://www.springframework.org/schema/context"
    xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx"
    xsi:schemaLocation="https://www.springframework.org/schema/beans
        classpath:/org/springframework/beans/factory/xml/spring-beans-4.0.xsd     
        https://www.springframework.org/schema/context
        classpath:/org/springframework/context/config/spring-context-4.0.xsd
        https://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        https://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx-4.0.xsd">     

    <!-- 掃描類包,將標注Spring注解的類自動轉化Bean,同時完成Bean的注入 -->
    <context:component-scan base-package="com.infotech.access.dao"/>

    <!-- 配置ucanaccess數據源 -->
    <bean  
        destroy-method="close" 
        p:driverClassName="net.ucanaccess.jdbc.UcanaccessDriver"
        p:url="jdbc:ucanaccess://\\\\172.16.30.106\\share\\data_be.mdb"
        p:username="admin"
        p:password="" />

    <!-- 配置事務管理器 -->
    <bean 
        
        p:dataSource-ref="dsmysql" />

    <!-- 配置JdbcTemplate  -->
    <bean  
        p:dataSource-ref="dsmysql" />

</beans>  

我們的Spring配置文件中,配置了一個dbcp管理的access數據庫的數據源,而且,為這個數據源配置了JdbcTemplate和事務管理器。

那麼我們為什麼要進行這種配置呢?

因為我們想使用Spring的JdbcTemplate訪問access數據庫,那麼為什麼要配置事務管理器呢?

哈哈,這就是問題的實質了,原因是我們寫完代碼以後要使用TestNG進行測試。讓我們重新看一看測試代碼:

package com.infotech.access.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.infotech.access.domain.CheckedList;
import static org.testng.Assert.*;

@ContextConfiguration("classpath*:/ruku-context.xml")
public class CheckedlistDaoTest extends <u>**AbstractTransactionalTestNGSpringContextTests**</u>{

    private CheckedlistDao cld;

    @Autowired
    public void setCheckedlistDao(CheckedlistDao cldlocal){
        this.cld = cldlocal;
    }


    @Test
    public void testAcess() {


       }

}

上麵就是TestNG的測試代碼,測試代碼中首先導入Spring配置文件ruku-congtext.xml,然後讓這個測試類extends AbstractTransactionalTestNGSpringContextTests
也就是讓我們的測試類擁有事務。
https://www.iteye.com/problems/100963
這篇博文介紹了TestNG中有關於事務的內容。
繼承AbstractTransactionalTestNGSpringContextTests類,test方法中的測試數據不會真的提交數據庫,他將在test方法執行完畢後進行回滾。如果你希望控製測試代碼的事務提交,可以通過一些annotation來完成。
如下代碼:


@Rollback(false)
@Test
    public void testAcess() {


       }

在你的TestNG測試類中將測試方法標注如上所示的annotation,測試事務就提交了,就可以進行數據的插入和刪除了哦

最後更新:2017-10-19 10:03:44

  上一篇:go  EB級別體量之下的高效數據賦能 ——雲棲大會阿裏大數據分論壇精彩演講2
  下一篇:go  探秘視頻編碼黑科技,窄帶高清2.0視覺模型及場景實戰