Spring使用DriverManagerDataSource和C3P0分別配置MySql6.0.6數據源


首先,看一下項目路徑

先說spring配置文件吧,這個比較重要

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
 9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
11 
12     <!-- 導入屬性文件 -->
13     <context:property-placeholder location="classpath:properties/jdbc.properties"/>
14 
15     <bean id="dataSourceSpring" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
16         <property name="username" value="${jdbc.user}"></property>
17         <property name="password" value="${jdbc.pwd}"></property>
18         <property name="url" value="${jdbc.url}"></property>
19         <property name="driverClassName" value="${jdbc.driver}"></property>
20     </bean>
21     <bean id="dataSourceC3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
22         <property name="user" value="root"></property>
23         <property name="password" value="1234"></property>
24         <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Hongkong"></property>
25         <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
26     </bean>
27 </beans>

bean的id使用不同的來進行區分,主要區別在於class指向不同,dataSourceSpring 並沒有連接池作用,另一個使用了C3P0進行配置

properties文件

1 jdbc.user=root
2 jdbc.pwd=1234
3 jdbc.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Hongkong
4 jdbc.driver=com.mysql.cj.jdbc.Driver

main方法文件

 1 package com.tse.test;
 2 
 3 import java.sql.SQLException;
 4 
 5 import javax.sql.DataSource;
 6 
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 public class Main {
11     public static void main(String[] args) throws SQLException {
12         ApplicationContext aContext = new ClassPathXmlApplicationContext("com/tse/spring/applicationContext_jdbc.xml");
13         DataSource dataSources = (DataSource) aContext.getBean("dataSourceSpring");
14         DataSource dataSources1 = (DataSource) aContext.getBean("dataSourceC3p0");
15         System.out.println(dataSources.getConnection());
16         System.out.println(dataSources1.getConnection());
17     }
18 }

執行結果

九月 13, 2018 12:04:35 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a6c5a9e: startup date [Thu Sep 13 12:04:35 CST 2018]; root of context hierarchy
九月 13, 2018 12:04:35 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/tse/spring/applicationContext_jdbc.xml]
九月 13, 2018 12:04:35 下午 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
信息: Loaded JDBC driver: com.mysql.cj.jdbc.Driver
九月 13, 2018 12:04:35 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
九月 13, 2018 12:04:35 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
Thu Sep 13 12:04:36 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
com.mysql.cj.jdbc.ConnectionImpl@1ffe63b9
九月 13, 2018 12:04:36 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge13c9x1m0nuop1yft6fk|2173f6d9, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge13c9x1m0nuop1yft6fk|2173f6d9, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Hongkong, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
Thu Sep 13 12:04:36 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Thu Sep 13 12:04:36 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Thu Sep 13 12:04:36 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
com.mchange.v2.c3p0.impl.NewProxyConnection@769f71a9

代碼上傳位置,請自行獲取

https://download.csdn.net/download/lijian0420/10664103


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM