用Hibernate3竟然不能写数据到数据库!

来源:百度知道 编辑:UC知道 时间:2024/07/02 01:02:19
我是采用的声明事务的方式。但是每次运行时候,数据写不进。也没有提示什么错误。我怀疑是不是事务没有提交。大家遇到过这样的事吗?怎么解决?
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>

</tx:attributes>
</tx:advice>

<!-- 哪些类的方法参与事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* org.yhq.binernate.sevices.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>

当然是事物没有提交才会出现这个情况
这也是面试的时候最经典的一道问题
没有提示错误,但是就是写不进数据库
可不就是事物没有提交,数据库里并没有真正写进数据
加个事物控制一切OK
或者写过滤器,在过滤器里在用户提交数据的时候就开始事物控制
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException
{
Session s = HibernateSessionFactory.getSession();
Transaction tx = s.beginTransaction();
try
{
arg2.doFilter(arg0, arg1);
tx.commit();
s.close();
} catch (Exception e)
{
tx.rollback();
}
}
别忘了在XML里配置一下
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>com.bluedot.web.filter.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

在hibernate.cfg.xml右上角properties