接口:
public interface StuDao {
public void insert();
public void delete();
}
public interface StuSer {
public void update();
}
实现:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.test.spring.interf.StuDao;
public class DaoImpl extends JdbcDaoSupport implements StuDao {
public void delete() {
JdbcTemplate jt= this.getJdbcTemplate();
String sql1 = "delete from student1 where id=1";
jt.update(sql1);
}
public void insert() {
JdbcTemplate jt= this.getJdbcTemplate();
String sql = "insert into student2 values (1,'jack','dd')";//这句话会出错
jt.update(sql);
}
}
public class StuSerImpl {
private DaoImpl dao;
public void update() {
try{
this.dao.delete();
this.dao.insert();
}catch(Exception e){
System.out.println("stuSerImpl-----"+e);
}
}
public DaoImpl getDao() {
return dao;
}
public void setDao(DaoImpl dao) {
this.dao = dao;
}
}
action:
public void setStuSerImpl(StuSerImpl stuSerImpl) {
this.stuSerImpl = stuSerImpl;
}
public StuSerImpl getStuSerImpl() {
return this.stuSerImpl;
}
private StuSerImpl stuSerImpl;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DForm dForm = (DForm) form; // TODO Auto-generated method stub
try{
stuSerImpl.update();
}catch(Exception e){
System.out.println("-------------"+e);
}
return null;
}
}
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://struts.apache.org/dtds/struts-config_1_1.dtd">
attribute="dForm"
name="dForm"
path="/d"
scope="request" type="org.springframework.web.struts.DelegatingActionProxy" />
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
**
-->
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@****:**
**
**
PROPAGATION_REQUIRED
[b]问题补充:[/b]
上面的写错了,是这样:
PROPAGATION_REQUIRED,-BadSqlGrammarException
[b]问题补充:[/b]
不做异常处理也不行啊。
PROPAGATION_REQUIRED
你这里写错啦,你只做了以de开头的失误处理
一般我是这样写的
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED,readOnly
只读失误的设置为PROPAGATION_REQUIRED,readOnly,其他的设置为PROPAGATION_REQUIRED
[quote] PROPAGATION_REQUIRED [/quote]
你只限定了de开头的有事务控制;
[quote]
}catch(Exception e){
System.out.println("stuSerImpl-----"+e);
}
[/quote]
你不做异常控制,就自然回滚了吧?
[quote]}catch(Exception e){
System.out.println("stuSerImpl-----"+e);
}[/quote]
你自己catch住了异常,当然不能回滚了。请将异常处理去除,并将事务配置为如下
PROPAGATION_REQUIRED,-Exception