1、首先要引入c3p0包和支持包,在hibernate的lib有个optional目录,里面有需要的包。
2、配置driverClass等相关属性,注意:property里面的name值不可乱写,必须是下图配置的那样,否则会报property 不受信赖异常。
3、使用mysql8.0.1版本数据库,配置jdbcUrl时要加时区等后缀,下面的配置可行,但还有其他配置方法,我不太理解为什么。
<!--dataSource Configuration -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT%2B8&useSSL=FALSE"></property>
<property name="user" value="weishao"></property>
<property name="password" value="123456"></property>
</bean>
4、重点:接下来是配置sessionFactory,引入配好的数据源,引入hibernate.cfg.xml(有些配置写在这文件,比如show_sql=true,映射文件等等,当然也可以都写在spring的配置文件),至此,是网上比较主流的一些配置, 都是基于成功的基础上。但是我就失败了,原因就是说连接不到数据库,在初始化c3p0连接池的时候停顿了好久,原因是Spring管理SessionFactory配置数据库连接的时候,默认找hibernateProperty里面的连接信息,所以要在sessionfactory bean里加入一个hibernateProperty属性,配置hibernate的连接数据库信息。
<!-- create sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<value>
hibernate.connection.driver_class com.mysql.cj.jdbc.Driver
hibernate.connection.username weishao
hibernate.connection.password 123456
hibernate.connection.url jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT%2B8&useSSL=FALSE
</value>
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
下面是未覆盖hibernateProperty连接配置的异常:
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource@3c098cfd[ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, allUsers -> [], autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 3db5e9f1, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 3c098cfd, idleConnectionTestPeriod -> -1, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT%2B8&useSSL=FALSE, maxIdleTime -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ]
七月 24, 2018 5:46:32 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@106e1cb1 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
java.sql.SQLException: Access denied for user 'sa'@'localhost' (using password: NO)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:107)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:113)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:728)
at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1258)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:368)
七月 24, 2018 5:46:32 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@ab9b7ea -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)