首页 > hibernate交spring事物托管后插入数据无效,不发送sql语句,不报错,但能查询到数据。

hibernate交spring事物托管后插入数据无效,不发送sql语句,不报错,但能查询到数据。

hibernate交spring事务托管后插入数据无效,不发送sql语句,不报错,但能查询到数据。
如果手动执行sessionFactory.getCurrentSession().flush()可以立即写入到数据库。
不手动的话无法自动事务提交。
怀疑是spring事务管理没起作用。
(学生党刚学框架,不妥当的地方还请明示)

dao层代码:

public class UserDao implements IUserDao{
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public boolean addUser(User user) {
        LogUtil.getLog().info("正在执行user插入操作~");
        try {
            sessionFactory.getCurrentSession().save(user);
        } catch (Exception e) {
            LogUtil.getLog().error(e);
            return false;
        }
        return true;
    }

controller层:

if(code.equals(checkCode)){
            if(userDao.addUser(user)){
                //将用户信息加进session中
                return "redirect:/returnIndex";
            }else{
                model.addAttribute("message", "系统异常,注册失败!");
                return "forward:/user/returnRegiste";
            }
        }else{
            model.addAttribute("message", "验证码错误!");
            return "forward:/user/returnRegiste";
        }

xml配置:

 <!-- c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
       destroy-method="close">
        <property name="user"><value>root</value></property>
        <property name="password"><value></value></property>
        <property name="minPoolSize"><value>20</value></property>
        <property name="maxPoolSize"><value>100</value></property>
        <property name="initialPoolSize"><value>20</value></property>
        <property name="driverClass"><value>com.mysql.jdbc.Driver</value></property>
        <property name="jdbcUrl"><value>jdbc:mysql://localhost:3306/db_shop?useUnicode=true&amp;characterEncoding=UTF-8</value></property>
    </bean>
    <!-- session工厂 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="annotatedClasses">
            <list>
                <value>edu.hnuc.entity.User</value>
            </list>
        </property>
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">edu.hnuc.util.UTF8CharacterDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 通知 -->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
    <!-- 切入点 -->
   <aop:config>
           <aop:pointcut expression="execution(* edu.hnuc.dao.impl.*.*(**))" id="myPoincut"/>
           <aop:advisor advice-ref="myAdvice" pointcut-ref="myPoincut"/>
   </aop:config>
   <!-- openSessionInView -->
   <mvc:interceptors>
        <bean class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
                <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
   </mvc:interceptors>
   
   
   

文件结构:


你配置文件里面都没有配置事务,怎么自动提交?肯定只能你自己手动在代码里面提交事务才行了
最简单的就是这样

<tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

然后在你dao方法上面加@Transactional
更新评论里面的回答

这样写

<aop:pointcut expression="execution(* edu.hnuc.dao.impl.*.*(..))" id="myPoincut"/>

1、既然用了springMVC和hibernate,为啥不用spring的hibernateTemplate呢?

2、你的spring配置文件中配置sessinFactory和transactionManager,但你的DAO中却自己从sessionFactory中获取session,而且没有相关的事物控制代码……我认为就是因为这个导致事物没有被提交。就好比你用了连接池,却自己用JDBCDriver创建了一个connection,那连接池肯定管理不了这个Connection啊,因为它都不知道有这么个连接……

最后给你一个建议:调试阶段的代码,e.printStackTrace()还是加上的好,而且最好加上throw e,这样让上游代码知道本层出了问题,否则,很可能因为人为的因素(比如忘了写“LogUtil.getLog().error(e);”或其他)导致异常消息被吞掉,如果发生这样吞异常的情况,定位问题将是十分繁琐的。

【热门文章】
【热门文章】