- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 struts2 - spring 3.2.2 和 mybatis。
首先我的要求是:
制作一个事务管理实用程序,其中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost;database=master;integratedSecurity=true;"/>
<property name="username" value="Jaydeep"/>
<property name="password" value="Acty#System123"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name='mapperLocations' value='classpath*:test/xml/*.xml' />
</bean>
<bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'>
<property name='basePackage' value='test.dao' />
</bean>
<bean id='sqlSession' class='org.mybatis.spring.SqlSessionTemplate'>
<constructor-arg index='0' ref='sqlSessionFactory' />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="serviceProvider"
class="DataServiceProvider">
<property name="sqlSession" ref="sqlSession" />
</bean>
<bean id="updutil" class="MyUpdateUtil">
<property name="serviceProvider" ref="serviceProvider"></property>
</bean>
</beans>
import org.apache.ibatis.session.SqlSession;
import test.dao.DepartmentMapper;
import test.dao.EmployeeMapper;
public class DataServiceProvider {
private SqlSession sqlSession;
public DepartmentMapper getDeptMapper() {
if(sqlSession != null)
return sqlSession.getMapper(DepartmentMapper.class);
else
System.out.println("session null");
return null;
}
public EmployeeMapper getEmpMapper() {
if(sqlSession != null)
return sqlSession.getMapper(EmployeeMapper.class);
else
System.out.println("session null");
return null;
}
public SqlSession getSqlSession() {
return sqlSession;
}
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
}
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.springframework.transaction.annotation.Transactional;
import test.model.Department;
@Transactional
public interface fooService {
public void update(boolean isThrow) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException;
public void insert(Department dept) throws IOException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException;
public void select() throws IOException;
}
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import test.model.Department;
import test.model.Employee;
import test.model.EmployeeExample;
@Transactional
public class MyUpdateUtil implements fooService {
public void update(boolean isThrow) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException {
System.out.println("Updating...................Transaction alive? ..." + StartTransAction.isActive());
if(isThrow)
throw new RuntimeException("simulate Error condition") ;
Employee record = new Employee();
EmployeeExample example = new EmployeeExample();
example.createCriteria().andDeptidEqualTo(1L);
record.setEmpid(1L);
record.setDeptid(1L);
record.setEmpname("jaydeep");
record.setSalary(BigDecimal.valueOf(2000));
getServiceProvider().getEmpMapper().updateByExampleWithBLOBs(record, example);
}
@Transactional
public void insert(Department dept) throws IOException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
System.out.println("Inserting....................Transaction alive? .." + StartTransAction.isActive());
getServiceProvider().getDeptMapper().insert(dept);
}
public void select() throws IOException {
System.out.println("Dept Info");
List<Department> deptList = getServiceProvider().getDeptMapper().selectByExampleWithBLOBs(null);
for(Department d : deptList) {
System.out.println("Dept ID: " + d.getDeptid());
System.out.println("Dept Name: " + d.getDeptname());
}
System.out.println("Emp Info");
List<Employee> empList = getServiceProvider().getEmpMapper().selectByExampleWithBLOBs(null);
for(Employee e : empList) {
System.out.println("Emp ID: " + e.getEmpid());
System.out.println("Dept ID: " + e.getDeptid());
System.out.println("Emp Name: " + e.getEmpname());
}
}
@Autowired(required=true)
private DataServiceProvider serviceProvider;
public DataServiceProvider getServiceProvider() {
return serviceProvider;
}
public void setServiceProvider(DataServiceProvider serviceProvider) {
this.serviceProvider = serviceProvider;
}
}
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import test.model.Department;
import com.opensymphony.xwork2.ActionSupport;
public class StartTransAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String execute() throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException {
Department dept = new Department();
dept.setDeptid(Long.valueOf(String.valueOf(new Random().nextInt(500))));
dept.setDeptname("esb");
System.out.println("before Insert..................Transaction alive? ...." + isActive());
try {
updutil.insert(dept);
System.out.println("After Insert..................Transaction alive? ...." + isActive());
updutil.select();
updutil.update(true);
} catch (Exception e) {
System.out.println(e.toString());
}finally{
System.out.println("After Update.................Transaction alive? ....." + isActive());
updutil.select();
}
return SUCCESS;
}
@Autowired
fooService updutil;
public fooService getUpdutil() {
return new MyUpdateUtil();
}
public void setUpdutil(fooService updutil) {
this.updutil = updutil;
}
private DataSourceTransactionManager transactionManager;
public DataSourceTransactionManager getTransactionManager() {
return transactionManager;
}
public void setTransactionManager(
DataSourceTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public static boolean isActive() throws IOException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
Class tsmClass = contextClassLoader.loadClass("org.springframework.transaction.support.TransactionSynchronizationManager");
Boolean isActive = (Boolean) tsmClass.getMethod("isActualTransactionActive", null).invoke(null, null);
return isActive;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- bean id="serviceProvider" class="DataServiceProvider"></bean-->
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost;database=master;integratedSecurity=true;"/>
<property name="username" value="Jaydeep"/>
<property name="password" value="Acty#System123"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name='mapperLocations' value='classpath*:test/xml/*.xml' />
</bean>
<bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'>
<property name='basePackage' value='test.dao' />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id='sqlSession' class='org.mybatis.spring.SqlSessionTemplate'>
<constructor-arg index='0' ref='sqlSessionFactory' />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="nestedTransactionAllowed" value="true" />
<property name="validateExistingTransaction" value="true" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index='0' ref='sqlSessionFactory' />
</bean>
<bean id="myService" class="service.MyService">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate" />
</bean>
</beans>
package com.acty;
import java.lang.reflect.InvocationTargetException;
import service.MyService;
import com.opensymphony.xwork2.ActionSupport;
public class StartTransAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String execute(){
myService.startOperations();
return SUCCESS;
}
public static boolean isActive() {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
Class tsmClass = null;
try {
tsmClass = contextClassLoader.loadClass("org.springframework.transaction.support.TransactionSynchronizationManager");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Boolean isActive = null;
try {
isActive = (Boolean) tsmClass.getMethod("isActualTransactionActive", null).invoke(null, null);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isActive;
}
private MyService myService;
public void setMyService(MyService myService) {
this.myService = myService;
}
}
package service;
import java.util.List;
import java.util.Scanner;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test.dao.DepartmentMapper;
import test.dao.EmployeeMapper;
import test.model.Department;
import test.model.Employee;
import test.model.EmployeeExample;
@Service
@EnableTransactionManagement
@Transactional(propagation=Propagation.REQUIRED)
public class MyService {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Transactional(propagation=Propagation.REQUIRED)
public void startOperations() {
DepartmentMapper deptMapper = sqlSessionTemplate.getMapper(DepartmentMapper.class);
EmployeeMapper empMapper = sqlSessionTemplate.getMapper(EmployeeMapper.class);
System.out.println("Before Insert Dept" + com.acty.StartTransAction.isActive());
this.select(deptMapper, empMapper);
Department dept = new Department();
//insert new dept
Scanner sc = new Scanner(System.in);
System.out.println("Enter dept id ");
dept.setDeptid(sc.nextLong());
System.out.println("Enter dept Name ");
dept.setDeptname(sc.next());
deptMapper.insert(dept);
System.out.println("After Insert Dept" + com.acty.StartTransAction.isActive());
this.select(deptMapper, empMapper);
this.select(deptMapper, empMapper);
//now update employee
EmployeeExample example = new EmployeeExample();
example.createCriteria().andEmpidEqualTo(1L);
Employee emp = new Employee();
emp.setEmpname("jjj");
try {
//empMapper.updateByExampleSelective(emp, example);
empMapper.updateByExampleWithBLOBs(emp, example);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("After Update Emp");
this.select(deptMapper, empMapper);
}
public void select(DepartmentMapper deptMapper, EmployeeMapper empMapper) {
System.out.println("\nDeptartment\n");
List<Department> deptList= deptMapper.selectByExampleWithBLOBs(null);
for(Department de : deptList) {
System.out.println(" Dept Id : " + de.getDeptid());
System.out.println(" Dept Name : " + de.getDeptname());
}
System.out.println("\nEmployee\n");
List<Employee> empList= empMapper.selectByExampleWithBLOBs(null);
for(Employee emp : empList) {
System.out.println(" Emp Id : " + emp.getEmpid());
System.out.println(" Emp Name : " + emp.getEmpname());
}
}
}
最佳答案
当您在更新中抛出异常时,第一个事务(插入您的行)早已结束并提交。您可能想要实现的是在 StartTransAction.execute
中的 try/catch 中运行整个 block 。作为单一交易。
一般来说,不建议在 dao 级别定义事务 - 以及您的 MyUpdateUtil
看起来像一个带有选择/插入/更新方法的 dao 对象。您应该在服务层管理事务。
首先,将这些行移到 MyUpdateUtil
中的新方法中。 :
@Transactional
public void insertAndUpdate(Department dept) {
this.insert(dept);
this.select();
this.update(true);
}
execute
尝试/捕获 block 。这将为您提供进一步完善代码的工作起点。
关于spring - 使用@Transactional : transaction get commited even if exception thrown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15404378/
我正在寻找一种方法来编写 Signed-off-by:当我提交时自动标记。 我尝试通过 .git/config 文件配置它 (Reference) .我把这些代码行: [alias] comm
我使用的是 visual studio 2013,在提交 C# 代码时我面临 3 个选项。我需要解释每个选项之间关于我的本地存储库与 GitHub 存储库发生的情况的差异。 选项 1 表示提交 选项
我刚从 classes12.jar 升级到新的 jdbc 驱动程序到 ojdbc7.jar 我的应用在使用 ojdbc7.jar 运行时抛出异常: java.sql.SQLException: Cou
我问的是 Oracle SQL*PLUS ... 最佳答案 没有 :-) Oracle says The WORK keyword is supported for compliance with s
我必须在许多分支、许多存储库上恢复对文件所做的更改。我知道我可以使用 git checkout 哈希文件名,然后推送该更改。 问题是,我只知道在我想要恢复的实际提交之前有两次提交。 我怎样才能在这之前
看起来很简单,但我就是不明白。我在我的应用程序的根目录中。 这是我的工作流程。 git add . git commit -m "added a new feature some files chan
假设我有一个 git 分支,在共享它之前的最后审查中,我发现了一些小错误,例如拼写错误。我想做的是将那个补丁应用为“修复”,但它实际上会影响许多提交,因此在最终历史记录中没有错误的迹象。 也就是说,如
当我运行hg commit时,Mercurial会为我的提交消息生成一个文件,如下所示: HG: Enter commit message. Lines beginning with 'HG:' a
我已经为项目创建了一个新的存储库,并向其中添加了一些现有的代码库 (llvm)。该代码库大约有 18,000 个文件,这使得我的初始提交花费了大量时间。 (阅读5小时) 有没有办法将这个巨大的提交分成
我在 git review 上得到以下内容: git review You are about to submit multiple commits. This is expected if you
我一直在寻找一种替代解决方案来压缩分支中的一系列提交。我过去所做的是使用 git rebase -i HEAD~然后选择哪个 promise 进行压缩。通常我 pick编辑最新的提交,并压缩其间的冗余
把玩Git和GitHub,我发现有时候一个 git commit -a 需要提交修改过的文件。 (此文件已添加到项目中)。 但有时候,只是一个 git commit 会起作用。如果使用 Mercuri
我正在努力思考 Git 的复杂性。 我使用“git clone [url here]”从 GitHub 下载了一个存储库。 我做了一些更改,尝试使用“git commit”提交它们。这似乎没有将更改推
当试图恢复到之前的提交时,我尝试了: git revert --no-commit 0766c053..HEAD 然而这给出了一个错误: empty commit set passed 问题错误是什么
我的存储库的历史非常复杂。我经常发现自己想知道过去的某个提交是“在”还是“可从”某个修订版(通常是我的一个头脑)“进入”或“可访问” 我该怎么做呢? 最佳答案 您可以使用 revsets syntax
我有:http://windows.github.com/ 我当前的项目有大约 20k 个文件,大约 150MB(并且不说它有多慢而且我现在什么也做不了)它甚至不允许我提交!我收到此错误:提交失败:无
我正在运行 postgres 9.2 服务器并有一个使用 psycopg 2.5 的 python 客户端。 我进行了一些测试,因为我在日志文件中遇到了很多警告:没有正在进行的事务条目。 我有一些代码
我的主要问题是总是执行 git commit -am 而不是 git add 是否有意义。然后是 git commit -m? 我知道 -am 表示它将添加修改后的 TRACKED 文件的所有更改。所
如果我想查看 之间的差异和工作目录 (WD),我运行 % git diff 这通常会做我想做的事,但如果 WD 包含在 时被跟踪的文件,它就会这样做。已创建,但现在(或在当前分支中)未被跟踪,则
我正在阅读有关 git 对象的信息:blob、树、提交、标签。为了更好地理解 git 的工作原理,我尝试了一些低级命令,如 write-tree 和 commit-tree。 mkdir 测试; cd
我是一名优秀的程序员,十分优秀!