spring,maven,jdbc ,sqlserver 开启事务

SqlSession sqlsession=DbHelper.sqlSessionFactory.openSession(false);
Transaction tra=new JdbcTransaction(sqlsession.getConnection());
try{
//这里执行扫描任务
}
catch(Exception e){
e.printStackTrace();
tra.rollback();
}
finally{
tra.close();
sqlsession.close();
}

DbHelper文件是这样的:

public static SqlSessionFactory sqlSessionFactory;
public static JdbcTransactionFactory trfactory;



public static void InitSqlSessionFactory() {
if(sqlSessionFactory==null){
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
sqlSessionFactory = (SqlSessionFactory)context.getBean("sqlSessionFactory");

}


if(trfactory==null){
trfactory=new JdbcTransactionFactory();
}

}

喜欢自己控制,不要搞注解

原文地址:https://www.cnblogs.com/aobama/p/4941753.html