1. 程式人生 > >《數據源與Spring》第一篇

《數據源與Spring》第一篇

配置 size run change abc username return sql time

平時開發中我們每天都會跟數據庫打交道,頁面上顯示的數字,圖片,語音,等等都存在某個地方,而我們就是要從那個地方拿到我們想要的。現在存儲數據的方式越來越多,多種多樣,但用的最多的還是關系數據庫。Spring 中有多種配置數據源方式,各有優缺點。

(1)DBCP

<dependency>
   <groupId>commons-dbcp</groupId>
   <artifactId>commons-dbcp</artifactId>
   <version>1.4</version>
</dependency>

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url"
   value="jdbc:mysql://1.1.0.9:3306/dis?useSSL=false"></property>
  <property name="username" value
="root"></property> <property name="password" value="123"></property> <property name="initialSize" value="5"></property> <property name="maxActive" value="10"></property> </bean>

(2)DriverManagerDataSource 和 SingleConnectionDataSource

<dependency
> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.0.0.RELEASE</version> </dependency>
<bean id="dataSource2"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://1.1.0.9:3306/dis"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
</bean>

<bean id="dataSource3"
        class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://1.1.0.9:3306/dis"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
</bean>

(3)C3P0

<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
    <bean id="dataSource5" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl"
            value="jdbc:mysql://1.1.0.9:3306/dis?useSSL=false"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="initialPoolSize" value="10" />
        <property name="maxIdleTime" value="60" />
        <property name="idleConnectionTestPeriod" value="60" />
        <property name="acquireIncrement" value="5" />
        <property name="acquireRetryAttempts" value="30" />
</bean>

(4)DRUID

<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.19</version>
</dependency>

    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url"
            value="jdbc:mysql://1.1.0.9:3306/dis?useSSL=false"></property>
        <property name="username" value="root"></property>
        <property name="password" value="abcd123"></property>
        <property name="filters" value="stat" />
        <property name="maxActive" value="20" />
        <property name="initialSize" value="2" />
        <property name="maxWait" value="60000" />
        <property name="minIdle" value="10" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="SELECT ‘1‘" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="maxOpenPreparedStatements" value="100" />
    </bean>

《數據源與Spring》第一篇