<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 241,  comments - 116,  trackbacks - 0
    使用jdbc連接數(shù)據(jù)庫(kù)所有功能都沒問題,發(fā)布到tomcat中也沒問題,可是如果使用tomcat的數(shù)據(jù)源,來(lái)連接數(shù)據(jù)庫(kù),開始很正常,但是刷新幾次就會(huì)出現(xiàn)這個(gè)異常……

      

             2008-04-26 22:35:40,812 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null

     2008-04-26 22:35:40,812 ERROR [org.hibernate.util.JDBCExceptionReporter] - Cannot get a connection, pool error Timeout waiting for idle object

     2008-04-26 22:35:40,812 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null

     2008-04-26 22:35:40,812 ERROR [org.hibernate.util.JDBCExceptionReporter] - Cannot get a connection, pool error Timeout waiting for idle object

     2008-04-26 22:35:40,812 ERROR [com.dao.MessageDAO] - find all failed

     org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [null]; error code [0]; Cannot get a connection, pool error Timeout waiting for idle object; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object

    Caused by:

    org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object

        at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:104)

        at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880) 

        at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)

    ……………………


    開始弄了半天也不知道怎么回事,后來(lái)發(fā)現(xiàn)是我自己寫的分頁(yè)代碼有問題……原來(lái)的代碼如下:


     1    /**
     2      * 自定義的,用屬性模糊查詢
     3      *
     4      * */
     5     public List find(String propertyName, Object value) {
     6 
     7         log.debug("finding Message instance with property: " + propertyName
     8 
     9               + ", value: " + value);
    10 
    11         try {
    12 
    13            String queryString = "from Message as model where model."
    14 
    15                                    + propertyName + " like "+value+" order by model.time desc";          
    16 
    17            return getHibernateTemplate().find(queryString);
    18 
    19         } catch (RuntimeException re) {
    20 
    21            log.error("find by property name failed", re);
    22 
    23            throw re;
    24 
    25         }
    26 
    27       }
    28 
    29 
    30     /**
    31      *
    32      * 自定義的方法,獲取指定頁(yè)的數(shù)據(jù)
    33      *
    34      * */
    35     public List gotoPage(int page,int pageSize){
    36 
    37        
    38 
    39         int totItem = this.findAll().size();//記錄總條數(shù)
    40 
    41         int pageNum = totItem / pageSize +1;//總頁(yè)數(shù)
    42 
    43         int begin = 0;//當(dāng)前起始記錄數(shù)
    44 
    45        
    46         begin=page*pageSize-pageSize+1//計(jì)算當(dāng)前起始位置
    47 
    48 
    49         Session s =this.getSession();
    50 
    51         String hql = "from Message message order by message.time desc";
    52 
    53         Query q =s.createQuery(hql);
    54 
    55         q.setFirstResult(begin);
    56 
    57         q.setMaxResults(pageSize);      
    58 
    59         return q.list();
    60 
    61     }

    在這句中:
                    Session s =this.getSession();

            String hql = "from Message message order by message.time desc";

            Query q =s.createQuery(hql);

       查詢數(shù)據(jù)時(shí),Spring并不能夠自動(dòng)管理連接,也就是說,在使用中這幾句代碼重視不段的獲取數(shù)據(jù)庫(kù)的連接,每調(diào)用一次就申請(qǐng)一個(gè)連接……直到 tomcat連接池中的連接耗盡……所以就再也申請(qǐng)不到連接了……出現(xiàn)了這個(gè)異常,解決辦法是使用事務(wù)來(lái)管理這段代碼,讓Spring自動(dòng)管理這段代碼中 申請(qǐng)的連接。我使用了Spring AOP自動(dòng)事務(wù)代理……配置文件如下……


     1     <bean id="JndiDataSource"
     2         class="org.springframework.jndi.JndiObjectFactoryBean">
     3         <property name="jndiName">
     4             <value>java:comp/env/SqlServer</value>
     5         </property>
     6         <property name="resourceRef">
     7             <value>true</value>
     8         </property>
     9     </bean>
    10 
    11     <!-- hibernate的會(huì)話工廠 -->
    12     <bean id="sessionFactory"
    13         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    14         <property name="dataSource">
    15             <ref bean="JndiDataSource"></ref>
    16         </property>
    17         <property name="hibernateProperties">
    18             <props>
    19                 <prop key="hibernate.dialect">
    20                     org.hibernate.dialect.SQLServerDialect
    21                 </prop>
    22                 <!-- 顯示SQL,為了方便測(cè)試 -->
    23                 <prop key="hibernate.show_sql">true</prop>
    24             </props>
    25         </property>
    26         <property name="mappingResources">
    27             <list><!-- 映射文件 -->
    28                 <value>./Message.hbm.xml</value>
    29                 <value>./Setting.hbm.xml</value>
    30                 <value>./Admin.hbm.xml</value>
    31             </list>
    32         </property>
    33     </bean>
    34 
    35     <!-- 事務(wù)管理器 -->
    36     <bean id="transactionManger"
    37         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    38         <property name="sessionFactory">
    39             <ref bean="sessionFactory" />
    40         </property>
    41     </bean>
    42 
    43     <!--   配置事務(wù)攔截器-->
    44 
    45     <bean id="transactionInterceptor"
    46         class="org.springframework.transaction.interceptor.TransactionInterceptor">
    47         <property name="transactionManager">
    48             <ref bean="transactionManger" />
    49         </property>
    50         <!--   下面定義事務(wù)傳播屬性-->
    51         <property name="transactionAttributes">
    52             <props>
    53                 <prop key="find*">PROPAGATION_REQUIRED</prop>
    54                 <prop key="delete*">PROPAGATION_REQUIRED</prop>
    55                 <prop key="save*">PROPAGATION_REQUIRED</prop>
    56                 <prop key="merge*">PROPAGATION_REQUIRED</prop>
    57                 <prop key="attach*">PROPAGATION_REQUIRED</prop>           
    58                 <prop key="gotoPage">PROPAGATION_REQUIRED</prop>
    59             </props>
    60         </property>
    61     </bean>
    62 
    63     <!--  自動(dòng)代理 -->
    64     <bean id="autoBeanNameProxyCreator"
    65         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    66         <property name="beanNames">
    67             <list>
    68                 <value>*DAO</value>            
    69                 <value>gotoPage</value>
    70                 <value>find</value>
    71             </list>
    72         </property>
    73         <property name="interceptorNames">
    74             <list>
    75                 <idref local="transactionInterceptor" />
    76             </list>
    77         </property>
    78         <!--  這里的配置是必須的,否則無(wú)法完成代理的類型轉(zhuǎn)化 這是使用CGLIB來(lái)生成代理 -->
    79         <property name="proxyTargetClass" value="true"/>
    80     </bean>
    81 
    82     <bean id="MessageDAO" class="com.dao.MessageDAO">
    83         <property name="sessionFactory">
    84             <ref bean="sessionFactory"></ref>
    85         </property>
    86     </bean>
    87 
    88     <bean id="SettingDAO" class="com.dao.SettingDAO">
    89         <property name="sessionFactory">
    90             <ref bean="sessionFactory"></ref>
    91         </property>
    92     </bean>
    93 
    94     <bean id="AdminDAO" class="com.dao.AdminDAO">
    95         <property name="sessionFactory">
    96             <ref bean="sessionFactory"></ref>
    97         </property>
    98     </bean>
    OK,問題成功解決!速度好像還快些!
    posted on 2008-12-02 20:21 墻頭草 閱讀(1102) 評(píng)論(0)  編輯  收藏 所屬分類: Spring
    人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運(yùn)專家
    主站蜘蛛池模板: 中国一级全黄的免费观看| 久久久久亚洲精品无码网址色欲| 中文字幕亚洲综合久久| 久久精品亚洲AV久久久无码| 99re视频精品全部免费| 亚洲日本va午夜中文字幕一区| 亚洲蜜芽在线精品一区| 日韩精品人妻系列无码专区免费 | 一级毛片免费观看不卡视频| 亚洲成av人在片观看| 深夜特黄a级毛片免费播放| 亚洲国产成人精品久久久国产成人一区二区三区综| 国产亚洲精品VA片在线播放| 永久免费看bbb| a毛片成人免费全部播放| 亚洲一区无码中文字幕| 午夜无码A级毛片免费视频| 亚洲欧洲中文日产| 免费观看成人毛片a片2008| 久久精品国产亚洲AV天海翼 | 成人区精品一区二区不卡亚洲| 免费亚洲视频在线观看| 国产成人啪精品视频免费网| 特级毛片A级毛片100免费播放| 麻豆国产精品免费视频| 中文字幕在线日亚洲9| 国产一级淫片免费播放| 两性色午夜视频免费网| 亚洲国产精品人久久电影| 国产青草视频在线观看免费影院| 久久亚洲精品中文字幕三区| 5555在线播放免费播放| 亚洲中文字幕久久精品无码A| 两个人看的www高清免费视频| 午夜网站免费版在线观看| 亚洲AV日韩综合一区| 亚洲熟妇无码另类久久久| 日本高清在线免费| 九九久久精品国产免费看小说 | 成人性做爰aaa片免费看| 91亚洲精品麻豆|