现在有个系统,spring+hibernate+struts +SQL2005 的数据库(开始是SQL2000)只要执行update 后 查询就死锁,数据库死了,要把tomcat停了才能继续运行。现在发现不了原因。郁闷的 看不到摸不着的。不过怀疑他的配置文件 有问题:原来工程里面是有2个配置文件的:hibernate.cfg.xml 和 hibernate1.cfg.xml  (多个1)
调用的方式:
protected HibernateOperations hibernateImpl = HibernateManagerFactory
.getInstance("hibernate.cfg.xml");
或者
private static TServiceSalvedFromServicTypeDao dao;
public static TServiceSalvedFromServicTypeDao getInstance() { if (dao == null) {
dao = new TServiceSalvedFromServicTypeDao();
dao.setProps("hibernate.cfg.xml");
}
return dao;
} // 建立数据库连接
public void getConnect(TServiceSalvedFromServicTypeDao obj) {
try {
jdbc = new JDBCManager(obj.getProps());
conn = jdbc.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}现在 把2个配置文件合并到个 hibernate.cfg.xml 里面  ,以前调用 hibernate1.cfg.xml的 全部replace   
hibernate.cfg.xml。 结果死的更频繁。原 配置文件内容:
<mapping class="com.dingtian.ccsa.back.salvation.minzheng.medicare.bo.TMzme" />
<mapping resource="com/dingtian/ccsa/back/salvation/stat/bo/TService.hbm.xml" />
有2中形势的配置方式。我 一直怀疑  2中配置方式放在一个工程里面可以么?而且还放一个hibernate.cfg.xml配置文件中
<mapping class="com.dingtian.ccsa.back.salvation.minzheng.medicare.bo.TMzme" />
这种配置形式就是 (直接写类里面的):  
@Column(name = "salvedtime", unique = false, nullable = true, insertable = true, updatable = true, length = 50)
public String getSalvedtime() {
return salvedtime;
}public void setSalvedtime(String salvedtime) {
this.salvedtime = salvedtime;
}<mapping resource="com/dingtian/ccsa/back/salvation/stat/bo/TService.hbm.xml" />
这种是 (写在xml文件中的)
  <id
        name="registerNo"
        type="java.lang.String"
        column="REGISTER_NO"
        unsaved-value="undefined"
    >
数据库连接 是配置在tamcat 中的,在tomcat/conf文件下 server.xml文件 
<Resource name = "jdbc/xwjzdb" 
type = "javax.sql.DataSource" 
driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
url = "jdbc:sqlserver://192.168.8.122:1433;databaseName=db0706"
username = "sa"
password = "666666"
maxWait = "4000"
maxActive = "200"
maxIdle = "30" />
那位 高人帮小弟看看 ,会是什么问题 ,或者有出现过 类似这种情况的请告诉下 ,或者就不是配置文件问题  是其他问题的?现在 只要更新数据库 然后自己刷新页面查询就会死锁。死锁以后 必须从启动tomcat 然后数据自己会回滚
  

解决方案 »

  1.   

    如果配置文件配置了事务,请检查事务配置
    如果配置文件未配置事务,请在数据库操作完成后提交,并释放相关资源,如果session,ResultSet等
      

  2.   

    我搜索了 所以的xml文件 没有发现事务的相关配置。估计是默认。
      

  3.   

    那就检查java代码中有数据库操作的部分。
      

  4.   

    java 代码也没有显示 开启事务
      

  5.   

    手动提交代码忘了的话上GOOGLE搜搜
      

  6.   

    public void bulkUpdate() {  
     Session session=this.getHibernateTemplate().getSessionFactory().openSession();  
     Transaction tx= session.beginTransaction();  
     tx.begin();  
     Query query= session.createQuery("Update Syction s set s.show='T' ");  
     query.executeUpdate();   
     tx.commit();  
    }  
      

  7.   


    hibernate 因该默认就是自动提交的吧,很少有见到代码 要手动提交的。
      

  8.   

    封装的代码里面 都有session.flush();
    应该是提交了
      

  9.   

    提点小建议:
    【现在 只要更新数据库 然后自己刷新页面查询就会死锁。死锁以后 必须从启动tomcat 然后数据自己会回滚 】
    第一次更新数据库后 可以查询数据是否更新到数据库。
       如果已经更新到数据库,查询分析器手动改改数据看可以改吗.(可以判断是否是数据表被锁定了)刷新的时候最好调试下 取得数据库连接的代码,和执行SQL文的代码,如果不能执行应该有异常信息出来的,
      

  10.   

    现在发现   在好多方法里面的代码并没有调用原系统封装好的 hibernate 方法(系统里面对hibernate 的一些常用方法都做了二次封装,都有调用 session .flush() 方法)而是自己 新建了一个session
     HibernateSession hsession = new HibernateSession();
     Session session = hsession.getSession();
    在执行了 session.savaorupdate(); 方法并没有 显示的调用 session.flush() 方法
     
    我都给一一查找出来 全部加上以后 好像还是 会出现死锁。开始加的时候也估计 不是这个问题,因为开始也是这样代码 有的时候还是能修改保存数据的。
    不过 session.flush() 这句 还是要调用的是吧?
    在我的电脑上 明显比在其他测试人员的电脑上死锁频繁。 我估计是 我的电脑开了myeclipse 内存使用 基本都在800+以上
    估计 这死锁和内存有关系 
      

  11.   

    关注,一样的问题。xml文件没有配置事务。
    而且更新后有update语句。并且界面上显示的是update之后的结果。但是数据库并没有
    真正更新。数据库查询也会死掉。重启tomcat,再查还是Update之前的结果
      

  12.   

    建议在spring的xml配置文件中增加事务管理的配置,应该可以解决这个问题。
      

  13.   

    各位大虾, hibernate都这么强,可以帮小妹推荐一下 senior Java Engineer: We offer a competitive compensation, paid vacations, employee health benefits. Great career advanced opportunities. Would you be interested in cooperate with us later? Pls solid your resume according to our template attached. You may check our senior positions about Java opens from the below brief JD, the positions are good projects related finance(Bank) system, and the position is technically high level with good promote and developing career path, also opportunities for abroad; hope to get your feedback ASAP, any recommendations are welcome! This is really an attractive position with more development and chances for abroad; hope to cooperate with you soon. 
    1.      Oral and written English; Must be able to communicate in English. 2.      4+ years experience in Java development. 3.      Proficient in Java, having experience in EJB, Hibernate, Spring 4.      Good problem solving skills and be able to follow-through the problem. CV or recommend to [email protected]
      

  14.   

    <?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.0.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <!-- 配置sessionFactory -->
    <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 local="sessionFactory"/>
    </property>
    </bean>
        
        <!-- 配置事务特性 -->       
        <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 (* com.xxx.manager.*.*(..))"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
        </aop:config>           
    </beans>
    将红色字体,更改为你需要配置那些类的方法进行事务管理的package path: