我喜欢自己设置 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 的依赖注入来从数据库中获取数据
iterface 包 de.hrw.mgmtDAO 的示例;
导入 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);
}