主题:初使用Spring的一些问题
时间: 2020-08-27来源:ITEYE
前景提要
相关推荐: 解惑 spring 嵌套事务 详解spring事务属性 Spring的事务管理难点剖析(3):事务方法嵌套调用的迷茫 Spring Boot从入门到进阶 IDEA开发工具下Spring Boot视频教程(下)整合篇 Spring Cloud Gateway初体验 史上最简单的 SpringCloud 教程 | 终章 使用Spring Boot开发Web项目
推荐群组: Spring之旅
更多相关推荐
Spring 初使用Spring + Hibernate开发,有些地方不明白, 但是有些地方不明白: 1. 使用事务管理器时,什么条件下开始事务?事务是什么时候开始?以及什么时候提交事务? 2. 若某方法定了事务,但开始时候执行了一大堆不包含任何数据库操作的处理,此时会开始时候吗? 3. 方法定义为:PROPAGATION_NOT_SUPPORTED或者PROPAGATION_SUPPORTS似乎都是一样的,即不要求一定在事务中执行,在当前存在事务的时候就按事务方式执行,否则以非事务方式执行。 4. PROPAGATION_REQUIRED以及PROPAGATION_REQUIRES_NEW说的似乎也是一样,即要求事务中执行,若不存在事务,则新建事务,否则在已有事务中执行。 5. 若某方法定义Method1()事务定义为:PROPAGATION_MANDATORY时,若只是单独运行会抛出异常,若在某方法method2(),此方法事务定义为PROPAGATION_REQUIRED,因为是存在了事务,所以执此时执行正常, 但是若方法method3(),被定义为PROPAGATION_NEVER,若在method2()调用method3(),按文档说明应该会抛出错误,但为何在实际执行中没有? 6. 隔离级别以及LockMode不起作用,为何?(使用MS SQL SERVER 2000个人版以及JTDS数据库驱动) 配置如下: <?xml version="1.0"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="sessionFactory" class="youngfriend.server.common.dao.YFSessionFactoryBean"> <property name="poolKey"> <value>YFSYS_MS</value><!--指定连接池名称--> </property> <property name="mappingResources"> <list><!--需加载的映射关系列表--> <value>entity/Cat.hbm.xml</value> <value>entity/Person.hbm.xml</value> <value>entity/Parent.hbm.xml</value> <value>entity/Child.hbm.xml</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="util" class="youngfriend.server.common.dao.DAOUtil"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="serviceTarget" class="test.Test1Impl"> <constructor-arg><ref bean="util"/></constructor-arg> </bean> <bean id="service" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="transactionManager"/> </property> <property name="target"> <ref local="serviceTarget"/> </property> <property name="transactionAttributes"> <props> <!--<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="someOtherBusinessMethod">PROPAGATION_REQUIRED</prop>--> <prop key="testOplock">PROPAGATION_REQUIRED</prop> <prop key="testTransaction">PROPAGATION_NESTED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans> Java程序如下: /* * */ package test; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import youngfriend.server.common.dao.DAOUtil; import entity.Cat; /** * * * @author * @version 1.0 2005-4-15 */ public class TestTransactionImpl implements TestTransaction { private static final Log log = LogFactory.getLog(TestTransactionImpl.class);; private DAOUtil util; /** * @param util */ public TestTransactionImpl(DAOUtil util); { super();; this.util = util; } /* (non-Javadoc); * @see test.TestTransaction#useMandatory(); */ public void useMandatory(); { doUpdate("使用强制式","CT2005041300064");; } /* (non-Javadoc); * @see test.TestTransaction#useNested(); */ public void useNested(); { doUpdate("使用嵌入式","CT2005041300065");; } /* (non-Javadoc); * @see test.TestTransaction#useNever(); */ public void useNever(); { doUpdate("NEVER","CT2005041300066");; } /* (non-Javadoc); * @see test.TestTransaction#useNot_supported(); */ public void useNot_supported(); { doUpdate("not-supported","CT2005041300067");; } /* (non-Javadoc); * @see test.TestTransaction#useRequired(); */ public void useRequired(); { doUpdate("Required","CT2005041300068");; } /* (non-Javadoc); * @see test.TestTransaction#useRequired_new(); */ public void useRequired_new(); { doUpdate("Required_new","CT2005041300069");; useNever();; } /* (non-Javadoc); * @see test.TestTransaction#useSupports(); */ public void useSupports(); { doUpdate("Supports","CT2005041300070");; } /* (non-Javadoc); * @see test.TestTransaction#useNotDefined(); */ public void useNotDefined(); { doUpdate("没有定义","CT2005041300071");; useRequired_new();; } /* (non-Javadoc); * @see test.TestTransaction#useReadOnly(); */ public void useReadOnly(); { doQry("只读");; doUpdate("只读","CT2005041300072");; } /** * 不与数据库打交道的代码 */ private void unrwDB();{ System.out.println("doing some thing withoutDB");; } private void doQry(String message);{ log.info(message);; unrwDB();; // log.info("目前事务:" + SysprocessesViwer.printCount(););; List list=util.find("from Cat cat where cat.sex='M'");; System.out.println("cat list:" + list);; // log.info("目前事务:" + SysprocessesViwer.printCount(););; } private void doUpdate(String message,String id);{ log.info(message);; unrwDB();; // log.info("目前事务:" + SysprocessesViwer.printCount(););; Cat cat=(Cat); util.get(Cat.class,id);; cat.setName(message);; util.update(cat);; // log.info("目前事务:" + SysprocessesViwer.printCount(););; } }

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行