本帖最后由 harkue 于 2011-04-13 16:19:02 编辑

解决方案 »

  1.   

    hibernate查询是不是有线程约束?
      

  2.   

    findByNumber  这个方法看看
      

  3.   


    public Order findByNumber(String orderNumber) {
    try {
    String hql = "from Order where orderNumber = '" + orderNumber + "'";
    Query query = getSession().createQuery(hql);
    log.info(hql);
    List list = query.list();
    if (list.size() > 0)
    return (Order) list.get(0);
    return null;
    } catch (RuntimeException re) {
    log.error("find order by orderNumber failed", re);
    throw re;
    }catch(Exception e){
    e.printStackTrace();
    }catch(Error er){
    er.printStackTrace();
    }
    return null;
    }
      

  4.   

    spring的配置文件贴出来这是一种情况:
    Dao有没有继承org.springframework.orm.hibernate3.support.HibernateDaoSupport呢?让spring帮你管hibernate事务,否则你的session不及时手动close掉,就会造成这种情况。
      

  5.   

    getSession 这个好像是需要关闭的
    Session session = getSession();
     try {
                String hql = "from Order where orderNumber = '" + orderNumber + "'";
                Query query = session.createQuery(hql);
                log.info(hql);
                List list = query.list();
                if (list.size() > 0)
                    return (Order) list.get(0);
                return null;
            } catch (RuntimeException re) {
                log.error("find order by orderNumber failed", re);
                throw re;
            }catch(Exception e){
                e.printStackTrace();
            }catch(Error er){
                er.printStackTrace();
            }
            finally{
    releaseSession(session);
    }
      

  6.   

    Dao继承org.springframework.orm.hibernate3.support.HibernateDaoSupport,getSession方法就是这个里面的。spring配置如下: <bean name="orderDAOImpl" class="com.logistic.dao.impl.OrderDAOImpl"
    scope="prototype">
    <property name="sessionFactory" ref="sf"></property>
    </bean> <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/logistic?useUnicode=true&amp;characterEncoding=utf-8"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </bean>

    <!-- 配置SessionFactory -->
    <bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>

    <!-- 配置事务管理器-->

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    <property name="sessionFactory">
    <ref bean="sf"/>
    </property>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
    <tx:method name="*" read-only="true"/>
    </tx:attributes>
    </tx:advice>


    <aop:config>
    <aop:pointcut id="allMethod" expression="execution(* dao.imp.*.*(..))"/>
    <aop:advisor pointcut-ref="allMethod" advice-ref="txAdvice"/>
    </aop:config>
    我按照6楼说的改了,也还是不行。
      

  7.   

    我测试几次,程序会在第8次循环的时候卡住。
            public void testFindByNumber() {
    for (int i = 0; i < 9; i++) {
    System.out.println("====begin=====" + i);
    Order order = orderDAO.findByNumber("DD1302182287233");
    System.out.println(order);
    System.out.println("====stop=====" + i);
    }
    }
    控制台输出:====begin=====0
    Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
    com.logistic.entity.Order@10c3a08
    ====stop=====0
    ====begin=====1
    Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
    com.logistic.entity.Order@102a0a5
    ====stop=====1
    ====begin=====2
    Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
    com.logistic.entity.Order@1d8d237
    ====stop=====2
    ====begin=====3
    Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
    com.logistic.entity.Order@538974
    ====stop=====3
    ====begin=====4
    Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
    com.logistic.entity.Order@1be8711
    ====stop=====4
    ====begin=====5
    Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
    com.logistic.entity.Order@1c8fb4b
    ====stop=====5
    ====begin=====6
    Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
    com.logistic.entity.Order@127f79d
    ====stop=====6
    ====begin=====7
    Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
    com.logistic.entity.Order@5db5ae
    ====stop=====7
    ====begin=====8