<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* org.wx.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config> 我在daoimpl类中分别用两种方法第一种:(dao中注入了sessionFactoryUtil 管理session也可以手动管理trancastion不过比较麻烦老是要在类中写代码):public class LoginDaoImpl  implements ILoginDao {
private SessionFactoryUtil sessionFactoryUtil;
public TUser getTUser(String username, String password) { return null;
} public void saveTUser(TUser user) {
sessionFactoryUtil.getSession().save(user);
sessionFactoryUtil.endTransaction(true);
}
public void setSessionFactoryUtil(SessionFactoryUtil sessionFactoryUtil) {
this.sessionFactoryUtil = sessionFactoryUtil;
}
}另外的是把//sessionFactoryUtil.endTransaction(true);注释掉了 public class LoginDaoImpl  implements ILoginDao {
private SessionFactoryUtil sessionFactoryUtil;
public TUser getTUser(String username, String password) { return null;
} public void saveTUser(TUser user) {
sessionFactoryUtil.getSession().save(user);
//sessionFactoryUtil.endTransaction(true);注释掉了就不执行insert语句
}
public void setSessionFactoryUtil(SessionFactoryUtil sessionFactoryUtil) {
this.sessionFactoryUtil = sessionFactoryUtil;
}
}其中的sessionFactoryUtil 为:public class SessionFactoryUtil {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
session  = sessionFactory.openSession();
transaction = session.beginTransaction();
} public void endTransaction(boolean commite) {
if (commite) {
transaction.commit();
} else {
transaction.rollback();
}
session.close();
} public Session getSession() {
return session;
} public Transaction getTransaction() {
return transaction;
} public SessionFactory getSessionFactory() {
return sessionFactory;
}
}第二种 不执行insert sql语句 为啥spring的aop事务不起作用,该怎么配置?

解决方案 »

  1.   

    配置文件没仔细看,不确定是否正确。不过你应该在LoginDaoImpl的头上加一个注解:@Transactional,这样他才能知道是对那个类进行事务管理。不用注解的方式也行,具体怎么写你自己查查吧。
      

  2.   

    不好意思,你是说AOP没有执行是吧?检查下你的xml文件头部是否引了AOP?
    <?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:context="http://www.springframework.org/schema/context"
    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-2.5.xsd
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">注意倒数第2句:“http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      

  3.   

    谢谢 我的意思是我配置了spring的食物管理保存的时候却不commit 
    配置文件的声明部份和你给的一样
      

  4.   


    <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-2.5.xsd
                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
      

  5.   

    配置文件貌似木有问题。LZ看看是不是2楼所说的你没有再xml头部加一些东西。
      

  6.   

    那楼主确定方法在这个包下面的类中么?org.wx.service.impl
      

  7.   

    那个aop 控制的事务 可以在任意层吗 我的是按照jsp--action--service--dao--database 我在service层中控制可以不?可以详细 点或者给个简单的例子 就用上面的那种aop 方式  谢谢
      

  8.   

    <aop:pointcut id="allManagerMethod" expression="execution(* org.wx.service.impl.*.*(..))" />
    这句话是说事物将织入到哪些方法里,
    你写的表达式是说事物将织入到org.wx.service.impl包下的任何类中的任何方法中
    LZ的LoginDaoImpl  类在org.wx.service.impl下么?
      

  9.   

    没有在org.wx.service.impl下面 
    LoginDaoImpl在org.wx.dao.impl下面
    我的包结构是:
    org.wx.action
    org.wx.dao
    org.wx.dao.impl
    org.wx.service
    org.wx.service.impl
    org.wx.bean
      

  10.   

    <aop:pointcut id="allManagerMethod" expression="execution(* org.wx.service.impl.*.*(..))" />
    LZ把这句改成
    <aop:pointcut id="allManagerMethod" expression="execution(* org.wx.dao.impl.*.*(..))" />
    这样再试试
      

  11.   

    用hibernate的session手动处理事务,没有抛出异常也保存成功了啊public void saveTUser(TUser user) {
            sessionFactoryUtil.getSession().save(user);
            sessionFactoryUtil.endTransaction(true);
        }改用spring的aop事务管理 把sessionFactoryUtil.endTransaction(true);注释掉后就不能保存了
      

  12.   

    applicationContext.xml <?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-2.5.xsd
                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
    </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="del*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    </tx:attributes>
    </tx:advice>
    <aop:config proxy-target-class="true">
    <aop:pointcut id="serviceMethod" expression="execution(* org.wx.service.impl.*.*(..))" />
    <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
    </aop:config> <bean id="sessionFactoryUtil" class="org.wx.dao.util.SessionFactoryUtil">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>
    <bean id="loginDaoImpl" class="org.wx.dao.impl.LoginDaoImpl">
    <property name="sessionFactoryUtil">
    <ref bean="sessionFactoryUtil"/>
    </property>
    </bean>
    <bean id="loginServiceImpl" class="org.wx.service.impl.LoginServiceImpl">
    <property name="loginDaoImpl">
    <ref bean="loginDaoImpl" />
    </property>
    </bean>
    <bean id="loginAction" class="org.wx.action.LoginAction" scope="prototype">
    <property name="loginServiceImpl">
    <ref bean="loginServiceImpl"/>
    </property>
    </bean>
    </beans>hibernate.cfg.xml<?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
    <session-factory>
    <property name="connection.username">bbs_web</property>
    <property name="connection.url">
    jdbc:oracle:thin:@127.0.0.1:1521:orcl
    </property>
    <property name="dialect">
    org.hibernate.dialect.Oracle9Dialect
    </property>
    <property name="myeclipse.connection.profile">
    oracleDriver
    </property>
    <property name="connection.password">wwwwww</property>
    <property name="connection.driver_class">
    oracle.jdbc.driver.OracleDriver
    </property>
    <property name="show_sql">true</property>
    <mapping resource="org/wx/bean/Banji.hbm.xml" />
    <mapping resource="org/wx/bean/Student.hbm.xml" />
    <mapping resource="org/wx/bean/TUser.hbm.xml" />
    </session-factory></hibernate-configuration>
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
        <constant name="struts.action.extension" value="do,action"/>
        <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />  
    <package name="struts2"  extends="struts-default">
    <action name="login" class="loginAction" method="execute">
    <result name="toList" type="redirect">/userList.jsp</result>
    <result name="fail" >/login.jsp</result>
    <result name="input" >/login.jsp</result>
    </action>
    </package>
    </struts>jsp界面<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     <%@ taglib prefix="s" uri="/struts-tags" %>
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     <html>
       <head>
         <title>登录</title>
         <metahttp-equiv="pragma" content="no-cache">
         <metahttp-equiv="cache-control" content="no-cache">
         <metahttp-equiv="expires" content="0">    
       </head>
       <body>
       <s:form name="form1" action="/login.action">
        <s:textfield name="username" label="username"></s:textfield>
        <s:password name="password" label="password"></s:password>
        <s:submit label="submit"></s:submit>
       </s:form>
        <s:actionerror/>
       </body>
     </html>action类package org.wx.action;import java.util.Iterator;import org.wx.bean.TUser;
    import org.wx.service.ILoginService;import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {

    private ILoginService loginServiceImpl;

    public String execute() throws Exception {
    System.out.println("---------execute-------");
    if (username != null && password != null) {
    TUser tuser = new TUser();
    tuser.setPassword(getPassword());
    tuser.setUsername(getUsername());
    loginServiceImpl.saveTUser(tuser);
    }
    return "toList";
    }
    public String userList(){
    Iterator<TUser> iterator = loginServiceImpl.getAllUsers();
    ActionContext.getContext().put("userList", iterator);
    return "list";
    }
    private String username;
    private String password; public String getUsername() {
    return username;
    } public void setUsername(String username) {
    this.username = username;
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    } public void validate() {
    username = getUsername();
    password = getPassword();
    if (password == null || password.length() == 0) {
    super.addActionError("密码不能为空!");
    }
    if (username == null||username.length() == 0 ) {
    super.addActionError("用户名不能为空!");
    }
    } public void setLoginServiceImpl(ILoginService loginServiceImpl) {
    this.loginServiceImpl = loginServiceImpl;
    }}service实现类package org.wx.service.impl;import java.util.Iterator;import org.wx.bean.TUser;
    import org.wx.dao.ILoginDao;
    import org.wx.service.ILoginService;public class LoginServiceImpl implements ILoginService { private ILoginDao loginDaoImpl ;

    public void setLoginDaoImpl(ILoginDao loginDaoImpl) {
    this.loginDaoImpl = loginDaoImpl;
    }
    public TUser getTUser(String username,String password) {

    return null;
    }

    public void deleteTuser(TUser user) {

    }
    public void saveTUser(TUser user) {
    System.out.println("---------LoginServiceImpl saveTuser-------");
    try {
    loginDaoImpl.saveTUser(user);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public void updateTuser(TUser user) {

    }
    public Iterator<TUser> getAllUsers() {
    return loginDaoImpl.getAllUsers();
    }
    public TUser getUserByID(Integer userid) {
    return null;
    }}dao实现类package org.wx.dao.impl;import java.util.Iterator;import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.wx.bean.TUser;
    import org.wx.dao.ILoginDao;
    import org.wx.dao.util.HibernateBase;
    import org.wx.dao.util.SessionFactoryUtil;public class LoginDaoImpl  implements ILoginDao {
    private SessionFactoryUtil sessionFactoryUtil;
    public TUser getTUser(String username, String password) { return null;
    } public void saveTUser(TUser user) throws Exception{
    System.out.println("---------LoginDaoImplsaveTUser-------");
    sessionFactoryUtil.getSession().save(user);
    //sessionFactoryUtil.endTransaction(true);把这里注释掉就不执行保存操作了为什么?
    } public void beginTransaction() {

    } public void endTransaction() {

    } public void setSessionFactoryUtil(SessionFactoryUtil sessionFactoryUtil) {
    this.sessionFactoryUtil = sessionFactoryUtil;
    } public Iterator<TUser> getAllUsers() {
    Query query = sessionFactoryUtil.getSession().createQuery("select u from TUser  u");
    Iterator<TUser> iterator =query.iterate();
    return iterator;
    } public TUser getUserByID(Integer userid) {
    // TODO Auto-generated method stub
    return null;
    }
    }sessionFactoryUtil类package org.wx.dao.util;import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;public class SessionFactoryUtil {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    this.session        = sessionFactory.openSession();
    this.transaction    = session.beginTransaction();
    } public void endTransaction(boolean commite) {
    if (commite) {
    transaction.commit();
    } else {
    transaction.rollback();
    }
    session.close();
    } public Session getSession() {
    return session;
    } public Transaction getTransaction() {
    return transaction;
    } public SessionFactory getSessionFactory() {
    return sessionFactory;
    }
    }
      

  13.   

    我也遇到了同样的问题,关注中,还有如果在dao.impl下还有子目录的话 
    配置成 dao.impl.*.*(..);可以嘛? 还是要具体到具体类的上一级目录呢?