NutzCN Logo
问答 与spring集成时的事务管理未生效
发布于 2250天前 作者 hbh3819 1898 次浏览 复制 上一个帖子 下一个帖子
标签:

spring配置文件如下:

	<!-- 导入nutzboot的spring定义 -->
	<bean id="nutz.uflo.helper" class="cn.com.unis.uflo.utils.UfloHelper" />
	<bean id="nutz.dataSource" class="javax.sql.DataSource" factory-bean="nutz.uflo.helper" factory-method="getDataSource"/>
	<bean id="nutz.conf" class="org.nutz.ioc.impl.PropertiesProxy" factory-bean="nutz.uflo.helper" factory-method="getPropertiesProxy"/>
	<bean id="nutz.spring.propertyPlaceHolder" class="cn.com.unis.uflo.utils.Nutz2SpringPropertyPlaceholder">
		<property name="conf" ref="nutz.conf"></property>
	</bean>
	
	<import resource="classpath:uflo-console-context.xml" />
	
	<bean id="uflo.environmentProvider" class="cn.com.unis.uflo.utils.UfloEnvironmentProvider" factory-method="getSelf">
		<property name="platformTransactionManager" ref="uflo.transactionManager" />
		<property name="sessionFactory" ref="uflo.sessionFactory" />
	</bean>
	
	<bean id="uflo.sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="nutz.dataSource" />
		<property name="packagesToScan">
			<list>
			<value>com.bstek.uflo.model*</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.DmDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">none</prop>
			</props>
		</property>
	</bean>
	
	<bean id="uflo.transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="uflo.sessionFactory"/>
	</bean>
	
	<bean id="springDaoRunner" class="cn.com.unis.uflo.plugin.SpringDaoRunner"></bean>
	
	<bean id="nutDao" class="org.nutz.dao.impl.NutDao">
	    <property name="dataSource" ref="nutz.dataSource"/>
	    <property name="runner" ref="springDaoRunner"/>
	</bean>
	 
	<bean id="officialService" class="cn.com.unis.uflo.service.impl.OfficialServiceImpl" factory-method="getSelf">
		<property name="dao" ref="nutDao"></property>
		<property name="transactionManager" ref="uflo.transactionManager"></property>
	</bean>
	
	<bean id="signReportAction" class="cn.com.unis.uflo.action.SignReportAction" factory-method="getSelf">
		<property name="officialService" ref="officialService"></property>
	</bean>

service实现类如下:

@IocBean(name = "officialService",factory="cn.com.unis.uflo.service.impl.OfficialServiceImpl#getSelf")
public class OfficialServiceImpl extends BaseServiceImpl implements IOfficialService{
	
	private static IOfficialService officialService = new OfficialServiceImpl();

	@Inject
	private ProcessService processService;
	
	@Inject
	private TaskService taskService;
	
	private PlatformTransactionManager transactionManager;
	
	public static IOfficialService getSelf() {
		return officialService;
	}
	
	public void setDao(Dao dao) {
		this.dao = dao;
	}
	
	public PlatformTransactionManager getTransactionManager() {
		return transactionManager;
	}

	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
       //事务未生效方法
        @Transactional
	public void SignDraft(SignReport signReport,String[] handler,String divisionLeader,String processKey) {
		ProcessInstance instance = getProcessInstance(signReport.getId());
		signReport.setDocHead("测试事务");
		Long currentTaskId = null;
		if(instance == null){
			//开启流程
			ProcessInstance processInstance = startProcess(null, Long.parseLong(processKey), signReport.getId());
			signReport.setProcessId(processInstance.getId());
			currentTaskId = getCurrentTaskId(signReport.getId(), CommonUtil.getUserCode());
			insertContainAllLinks(signReport);
		}else{
			currentTaskId = getCurrentTaskId(signReport.getId(), CommonUtil.getUserCode());
			updateSignReportAllLinks(signReport);
		}
		Map<String,Object> variables = new HashMap<String, Object>();
		if(handler != null && handler.length > 0){
			variables.put(Constant.handlerKey, Arrays.asList(handler));
			variables.put(Constant.signRepSponsorDepeHeadKey, Arrays.asList(handler));
		}
		variables.put(Constant.divisionLeaderKey, divisionLeader);
		variables.put(Constant.starterKey, CommonUtil.getUserCode());
		variables.put(Constant.deptCodeKey, CommonUtil.getDeptCode());
		int i = 1/0;
		startAndCompleteTask(currentTaskId, variables);
	}

}

action如下:
@IocBean(factory="cn.com.unis.uflo.action.SignReportAction#getSelf")
@At({"/signReport"})
public class SignReportAction extends CommonAction{

private final Log log = Logs.getLog(getClass());

private static CommonAction signReportAction = new SignReportAction();

private String processKey = "128353";

private IOfficialService officialService;

public static CommonAction getSelf() {
    return signReportAction;
}

public IOfficialService getOfficialService() {
    return officialService;
}

public void setOfficialService(IOfficialService officialService) {
    this.officialService = officialService;
}

   @At("/completeDraft")
@Aop({"processLogInterceptor"})
@ProcessLogAnno(operationType ="发起签报", processType = Constant.SIGN_REPORT_PROCESS_TYPE)
public Object completeDraft(@Param("::s.")SignReport signReport,String[] handler,String divisionLeader){
    try {
       signReport = officialService.preUpdateSignReportLinks(signReport);
       signReport.setStatus(SignReportStatus.approv.val());
       officialService.SignDraft(signReport, handler, divisionLeader, processKey);
       return LayUtil.success();
    } catch (Exception e) {
       if(log.isErrorEnabled()) {
         log.errorf("流程更新失败:%s", new Object[] { e.getMessage() });
       }
       return LayUtil.fail("流程更新失败:" + e.getMessage());
    }
}

}

9 回复

在spring配置文件中开启了声明式配置

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

出现异常可以回滚,但是无法获取到同一事务内之前操作更新的数据

	@Transactional
	public void SignDraft(SignReport signReport,String[] handler,String divisionLeader,String processKey) {
		ProcessInstance instance = getProcessInstance(signReport.getId());
		signReport.setDocHead("测试事务1");
		Long currentTaskId = null;
		if(instance == null){
			ProcessInstance processInstance = startProcess(null, Long.parseLong(processKey), signReport.getId());
			signReport.setProcessId(processInstance.getId());
			//无法查询到前一次操作插入的结果:startProcess(null, Long.parseLong(processKey), signReport.getId());
			currentTaskId = getCurrentTaskId(signReport.getId(), CommonUtil.getUserCode());
			insertContainAllLinks(signReport);
		}else{
			currentTaskId = getCurrentTaskId(signReport.getId(), CommonUtil.getUserCode());
			updateSignReportAllLinks(signReport);
		}
		Map<String,Object> variables = new HashMap<String, Object>();
		if(handler != null && handler.length > 0){
			variables.put(Constant.handlerKey, Arrays.asList(handler));
			variables.put(Constant.signRepSponsorDepeHeadKey, Arrays.asList(handler));
		}
		variables.put(Constant.divisionLeaderKey, divisionLeader);
		variables.put(Constant.starterKey, CommonUtil.getUserCode());
		variables.put(Constant.deptCodeKey, CommonUtil.getDeptCode());
		//抛出异常
		int i = 1/0;
		startAndCompleteTask(currentTaskId, variables);
	}
	public Long getCurrentTaskId(String businessId,String assignee) {
		String sqlStr = "select * from $tableName where BUSINESS_ID_ = @id and END_DATE_ is null and STATE_ = 'Created'";
		if(assignee != null && assignee != "") {
			sqlStr += " and ASSIGNEE_ = '"+assignee+"'";
		}
		final Sql sql = Sqls.createf(sqlStr);
		sql.vars().set("tableName", "UFLO_TASK");
		sql.params().set("id", businessId);
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
						Long s = 0l;
						if(rs.next())
							s = rs.getLong("ID_");
					return s;
					}
		});
		dao.execute(sql);
		Long a = sql.getLong();
		return a;
	}

能回滚就是生效了吧

但是实际同一个service中的方法前一次对数据库的插入操作后面无法查询到,是否还是不在同一个事务内?

别挣扎了。。。

nutz ioc并不理会spring的注解,那些事务不会生效的

我就是想让这个方法内的操作处于同一个事务中,就没办法嘛。。

如果是从入口方法开始算事务开始,基本无解,uflo2没有单独的事物集成接口

nutz层的事务没法传递过去,除非给spring写个事务管理器

好吧,springDaoRunner只能保证事务的原子性?

springDaoRunner 不能保证任何的东西, 那是把spring作用域下的NutDao的事务交给spring管理

添加回复
请先登陆
回到顶部