c3p0连接池配置

<?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:context="http://www.springframework.org/schema/context"
    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.5.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context-2.5.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- 用注解方法注入bean 上边schemaLocation的三条语句顺序很重要,否则报错 -->
    <context:annotation-config />
    <context:component-scan base-package="com.cn.smy" />
    
    <!-- 数据库连接池 -->
    <!-- <property name="query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"></property> -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/school?useUnicode=true&amp;characterEncoding=UTF-8" />
        <property name="user" value="root" />
        <property name="password" value="root" />
        <!-- <property name="connection.provider_class" value="org.hibernate.c3p0.internal.C3P0ConnectionProvider"></property> --><!-- //c3p0连接池 -->
         <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->    
        <property name="initialPoolSize" value="3" />  
       <!--  <property name="c3p0.acquire_increment" value="5"/> -->
       <!--  <property name="idleConnectionTestPeriod" value="60"/>  --><!-- //设定的时间间隔去自动校验链接对象并销毁timeout的 -->        
        <property name="maxPoolSize" value="100"/><!-- //最大连接数 -->
        <property name="minPoolSize" value="15" /><!-- //最小连接数 -->
        <property name="maxStatements" value="100"/><!-- //JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。
        但由于预缓存的statements属于单个 connection而不是整个连接池。
        所以设置这个参数需要考虑到多方面的因素。如果maxStatements与 maxStatementsPerConnection均为0,则缓存被关闭 -->
        <!-- <property name="c3p0.timeout" value="90"/>//连接超时时间
        
        <property name="hibernate.show_sql" value="true"/>//显示hibernate对数据库操作语句
        <property name="hibernate.format_sql" value="true"/>//格式化Hibernate的SQL输出语句
        <property name="hibernate.hbm2ddl.auto" value="update"/> -->
    </bean>    

    <!-- 配置sessionFactory ,数据库配置在hibernate.cfg.xml中-->
    <!--LocalSessionFactoryBean 加载bean方式 <mapping resource="com/model/User.hbm.xml"/>
        AnnotationSessionFactoryBean 加载bean方式 <mapping class="com.model.User"/> ,它主要功能是取消了hbm.xml文件
     -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 配置实体描述文件 -->
        <property name="mappingResources">
            <list>
                <value>com/cn/smy/Xml/UsersBean.xml</value>
                <value>com/cn/smy/Xml/Home.xml</value>
            </list>
        </property>
        <!--扫描com.cuangwu包下以及子包种有 @Service @Controller @Repository @Component  注解的类,一旦发现,则将其纳入到spring容器中管理 
            此spring.jar必须是 Spring2.5以上版本的,因为,Spring2.5之前org.springframework.orm.hibernate3.LocalSessionFactoryBean类中,
            并没有 packageToScan 这个属性,只有mappingResuorces这个属性。而packageToScan这个属性正是映射包中的类,而mappingResuorces只是映射某个文件。-->
        <!-- <property name="packagesToScan" > <list> <value>com.model</value> 
            </list> </property> -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbn2dd1.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- 乱码解决 -->
            <!--     <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> -->
            </props>
        </property>
    </bean>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" /> 
            <tx:method name="update*" propagation="REQUIRED" /> 
        </tx:attributes>
    </tx:advice>
    <!-- aop代理设置--> 
    <aop:config>
        <aop:pointcut expression="execution(public * com.cn.smy.ServiceImpl.*.*(..))"
            id="myPointcut" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
    </aop:config>

</beans>

开始报错如下,需更换c3p0包 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:261) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:225) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) at test.UserTest01.save(UserTest01.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.connection.C3P0ConnectionProvider] at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:197) at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:120) at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:55) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:105) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:251) ... 34 more Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.connection.C3P0ConnectionProvider] as strategy [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:128) at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:194) ... 38 more
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值