zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    “.NET研究”带你走进缓存世界 狼人:
    晚绑定场景下对象属性赋值和取值可以不需要Pro“.NET研究”pertyInfo 狼人:
    一“.NET研究”句代码实现批量数据绑定[下篇] 狼人:
    使用WC“.NET研究”F实现SOA面向服务编程——简单的WCF开发实例 狼人:
    在ASP.NET MVC3中使用“.NET研究”EFCodeFirst 1.0 狼人:
    一句代码实现批量“.NET研究”数据绑定[下篇] 狼人:
    在ASP.NET M“.NET研究”VC3 中利用JSONP跨域登录WEB系统 狼人:
    一句代码实现批量数“.NET研究”据绑定[上篇] 狼人:
    转发重定向Servlet之请求转发与重定向区别
    估计实例R语言: 极大似然估计实例
  • 原文地址:https://www.cnblogs.com/gode/p/9640360.html
Copyright © 2011-2022 走看看