如何使用Spring和MyBatis设置事务隔离级别

时间:2015-05-30 20:13:56

标签: spring mybatis isolation-level transaction-isolation spring-mybatis

我喜欢自己设置Isolationlevel,使用Spring Framework中的transactionmanager和myBatis。我正在尝试很多教程,但没有任何效果。

我的应用程序是作为MVC模式构建的,这意味着我有来自mybatis和控制器类的依赖注入的视图,模型,接口。

我希望有人可以给我建议我是mybatis和spring的新手。整个应用程序运行良好,但我喜欢接管隔离级别的控制。

This is the spring-configuration.xml file
        <!--<mybatis-spring:scan base-package="de.hrw.model.**"/> -->
    <mybatis-spring:scan base-package="de.hrw.*" />
    <context:component-scan base-package="de.hrw.*" />



     <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/carrental">
        </property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="autoCommit" value="false"></property>
        <property name="registerMbeans" value="true"></property>
        <property name="transactionIsolation"
            value="TRANSACTION_SERIALIZABLE">
        </property> 
    </bean>


    <bean id="sqlSessionFactoryBean"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property> 
        <property name="configLocation" value="classpath:mybatis-config.xml">
        </property>     
    </bean>

    <bean id="carController" class="de.hrw.controller.CarController">
        <property name="transactionManager" ref="transactionManager" /> 
    </bean>

    <bean id="carSearchView" class="de.hrw.view.CarSearchView">
    </bean>


    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

我正在使用mybatis的dependecy-injection来从数据库获取数据

表面的例子 包de.hrw.mgmtDAO;

import java.util.List;

import de.hrw.model.CarModel;


public interface ICarMgmt {
    public CarModel selectCarById(final int carId);     
    public List<CarModel> selectAllCars(); 
}

这是包含视图(框架)

的主类
public class Carrental_main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

          ApplicationContext context = 
                  new ClassPathXmlApplicationContext("spring-config.xml");

          CarController carController = (CarController) context.getBean("carController");

          carController.openSearchView();
          carController.getCarSearchView().setVisible(true);
    }       

}

这是控制器。在这里,我尝试将隔离级别设置为SERIALIZABLE,但始终设置为默认值(-1)

@Transactional(propagation=Propagation.REQUIRES_NEW , isolation = Isolation.SERIALIZABLE)
public class CarController {
    @Autowired
    private ICarMgmt carMgmt;

private CarSearchView carSearchView;

private ApplicationContext applicationContext;
@Autowired
private PlatformTransactionManager transactionManager;

private TransactionStatus transactionStatus;

private TransactionDefinition defaultTransactionDefinition;

private DataSource dataSource;


public void openSearchView() {
    this.setApplicationContext();
    this.setDefaultTransactionDefinition();
    this.setTransactionStatus();

    this.carSearchView = (CarSearchView) applicationContext
            .getBean("carSearchView");


    try {

        List<CarModel> carList = carMgmt.selectAllCars();


        // this.carSearchView.setResultList(carList);
        this.carSearchView.setLabelList(carList);


        this.carSearchView.createTextFieldList();
        this.carSearchView.createLabelFieldList();


        transactionManager.commit(transactionStatus);

    } catch (DataAccessException e) {
        System.out.println("Error in creating record, rolling back");
        transactionManager.rollback(transactionStatus);
        throw e;
    }
}

 public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;

       }

public void setDefaultTransactionDefinition() {
    this.defaultTransactionDefinition = new DefaultTransactionDefinition();
}

public void setApplicationContext() {
    applicationContext = new ClassPathXmlApplicationContext(
            "spring-config.xml");
}

public void setTransactionManager(
        PlatformTransactionManager transactionManager) {
    this.transactionManager = transactionManager;
}

public void setTransactionStatus() {
    this.transactionStatus = transactionManager.getTransaction(defaultTransactionDefinition);

}

2 个答案:

答案 0 :(得分:1)

我终于找到了解决方案。我将控制器中的TransactionDefinition对象更改为DefaultTransactionDefinition对象

private DefaultTransactionDefinition defaultTransactionDefinition;

前者是

private TransactionDefinition defaultTransactionDefinition;

TransactionDefinition 并未提供任何设置方法。我很想知道,因为在文档中我找到了设置隔离级别的方法,但这些方法只是由 DefaultTransactionDefinition 提供的。在我发现这个失败后,我将以下内容添加到代码行中,它最终起作用

defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
defaultTransactionDefinition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_REPEATABLE_READ); 

谢谢你的所有建议。如果有人知道一个非常好的MyBatis + Spring教程和交易经理,请发布一个链接:D

答案 1 :(得分:0)

您可以在mapper界面中应用如下所示的事务(尽管建议为类应用事务注释,但是在mybatis中,接口中定义的事务将应用于代理类)

import java.util.List;
import de.hrw.model.CarModel;

@Transactional
public interface ICarMgmt {

    @Transactional(isolation = Isolation.SERIALIZABLE)
    public CarModel selectCarById(final int carId);

    public List<CarModel> selectAllCars(); 
}