Mybatis scriptRunner无法回滚

时间:2019-11-19 08:25:29

标签: java mysql mybatis

使用scriptrunner运行sql文件,当发生错误时,我希望sql可以回滚,但是失败。
请帮助我。

Java代码:

try {
    Class.forName(driver);
    conn = DriverManager.getConnection(url, userName, password);
    conn.setAutoCommit(false);
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setAutoCommit(false);
    runner.setStopOnError(true);
    runner.setSendFullScript(false);
    runner.setDelimiter(";");
    runner.setFullLineDelimiter(false);
    runner.setLogWriter(null);

    Resource resource = new ClassPathResource(sqlFileName);
    File file = resource.getFile();
    runner.runScript(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));

    conn.commit();
} catch (Exception e) {
    conn.rollback();
    error = e;
} finally {
    close(conn);
}

sql文件:

create table daiql_test2 (
    id int,
    name varchar(100)
);

insert into daiql_test2 (id, name) value (1, 'test1');
update daiql_test2 set name = 'test2' where id = 1;

gold bless no bug...;

我使用的是mysql数据库,但它不起作用,因此作为oracle ...
DDL不仅无法回滚,而且DML也无法回滚。
这里有一些代码错误吗?

MySQL版本:

select version();

5.7.20

引擎:

show table status where name= 'daiql_test2';

Name    Engine  Version Row_format  Rows    Avg_row_length  Data_length Max_data_length Index_length    Data_free   Auto_increment  Create_time Update_time 
daiql_test2 InnoDB  10  Dynamic 4   4096    16384   0   0   0       2019-11-19 15:56:47 2019-11-20 09:20:05     utf8_unicode_ci 

pom.xml中的Mybatis和Connection / J:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.daiql</groupId>
    <artifactId>scriptrunner</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>scriptrunner</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>mysql</groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--            <version>5.1.47</version>-->
<!--        </dependency>-->

        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>  

application.yml:

spring:
  application:
    name: scriptrunner
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/daiqltest?userUnicode=true&characterEncoding=UTF8&useSSL=false
    username: root
    password: abcdef
    druid:
      filters: stat
      max-active: 20
      initial-size: 5
      max-wait: 60000
      min-idle: 5
      time-between-eviction-runsMillis: 60000
      min-evictable-idle-timeMillis: 300000
      validation-query: select 'x' FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

0 个答案:

没有答案