org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool er

 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

  查询数据时,Spring并不能够自动管理连接,也就是说,在使用中这几句代码重视不段的获取数据库的连接,每调用一次就申请一个连接……直到 tomcat连接池中的连接耗尽……所以就再也申请不到连接了……出现了这个异常,解决办法是使用事务来管理这段代码,让Spring自动管理这段代码中 申请的连接。我使用了Spring AOP自动事务代理……配置文件如下……

<!-- 来自JNDI的tomcat数据源,有连接池。但是用起来怎么就释放不了连接呢?程序只知道申请,不知道释放! -->

    <bean id="JndiDataSource"

        class="org.springframework.jndi.JndiObjectFactoryBean">

        <property name="jndiName">

            <value>java:comp/env/SqlServer</value>

        </property>

        <property name="resourceRef">

            <value>true</value>

        </property>

    </bean>

    <!-- hibernate的会话工厂 -->

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource">

            <ref bean="JndiDataSource"></ref>

        </property>

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">

                    org.hibernate.dialect.SQLServerDialect

                </prop>

                <!-- 显示SQL,为了方便测试 -->

                <prop key="hibernate.show_sql">true</prop>

            </props>

        </property>

        <property name="mappingResources">

            <list><!-- 映射文件 -->

                <value>./Message.hbm.xml</value>

                <value>./Setting.hbm.xml</value>

                <value>./Admin.hbm.xml</value>

            </list>

        </property>

    </bean>

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

    <bean id="transactionManger"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory">

            <ref bean="sessionFactory" />

        </property>

    </bean>

    <!--   配置事务拦截器-->

    <bean id="transactionInterceptor"

        class="org.springframework.transaction.interceptor.TransactionInterceptor">

        <property name="transactionManager">

            <ref bean="transactionManger" />

        </property>

        <!--   下面定义事务传播属性-->

        <property name="transactionAttributes">

            <props>

                <prop key="find*">PROPAGATION_REQUIRED</prop>

                <prop key="delete*">PROPAGATION_REQUIRED</prop>

                <prop key="save*">PROPAGATION_REQUIRED</prop>

                <prop key="merge*">PROPAGATION_REQUIRED</prop>

                <prop key="attach*">PROPAGATION_REQUIRED</prop>               

                <prop key="gotoPage">PROPAGATION_REQUIRED</prop>
<!--这里配置的事务管理相当重要, 在数据层所有的方法必须按照上面的格式进行编写-->
            </props>

        </property>

    </bean>

    <!--  自动代理 -->

    <bean id="autoBeanNameProxyCreator"

        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

        <property name="beanNames">

            <list>

                <value>*DAO</value>              

                <value>gotoPage</value>

                <value>find</value>

            </list>

        </property>

        <property name="interceptorNames">

            <list>

                <idref local="transactionInterceptor" />

            </list>

        </property>

       

        <!--  这里的配置是必须的,否则无法完成代理的类型转化

        这是使用CGLIB来生成代理

        -->

        <property name="proxyTargetClass" value="true"/>

    </bean>

    <bean id="MessageDAO" class="com.dao.MessageDAO">

        <property name="sessionFactory">

            <ref bean="sessionFactory"></ref>

        </property>

    </bean>

    <bean id="SettingDAO" class="com.dao.SettingDAO">

        <property name="sessionFactory">

            <ref bean="sessionFactory"></ref>

        </property>

    </bean>

    <bean id="AdminDAO" class="com.dao.AdminDAO">

        <property name="sessionFactory">

            <ref bean="sessionFactory"></ref>

        </property>

    </bean>

</beans>

 敬请关注  大神网  获取更多 编程资讯

 

你可能感兴趣的:(springmvc3.0)