• SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource


    一、概述

    1.Spring offers several options for configuring data-source beans in your Spring application, including these:

     Data sources that are defined by a JDBC driver
     Data sources that are looked up by JNDI
     Data sources that pool connections

    2.

    二、四种方式获取DataSource

    1. Using JNDI data sources

    (1)java方式

    1 @Bean
    2 public JndiObjectFactoryBean dataSource() {
    3     JndiObjectFactoryBean jndiObjectFB = new JndiObjectFactoryBean();
    4     jndiObjectFB.setJndiName("jdbc/SpittrDS");
    5     jndiObjectFB.setResourceRef(true);
    6     jndiObjectFB.setProxyInterface(javax.sql.DataSource.class);
    7     return jndiObjectFB;
    8 }

    (2)xml方式

    1 <jee:jndi-lookup id="dataSource" jndi-name="/jdbc/SpitterDS" resource-ref="true" />

    The jndi-name attribute is used to specify the name of the resource in JNDI . If only the jndi-name property is set, then the data source will be looked up using the name given as is. But if the application is running in a Java application server, you’ll want to set the resource-ref property to true so that the value given in jndi-name will be prepended with java:comp/env/

    2.Using a pooled data source

    (1)java方式

     1 @Bean
     2 public BasicDataSource dataSource() {
     3     BasicDataSource ds = new BasicDataSource();
     4     ds.setDriverClassName("org.h2.Driver");
     5     ds.setUrl("jdbc:h2:tcp://localhost/~/spitter");
     6     ds.setUsername("sa");
     7     ds.setPassword("");
     8     ds.setInitialSize(5);
     9     ds.setMaxActive(10);
    10     return ds;
    11 }

    (2)xml方式

    1 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    2   p:driverClassName="org.h2.Driver"
    3   p:url="jdbc:h2:tcp://localhost/~/spitter"
    4   p:username="sa"
    5   p:password=""
    6   p:initialSize="5"
    7   p:maxActive="10" />

    3.Using JDBC driver-based data sources(不支持连接池)

    有3种选择:

     DriverManagerDataSource —Returns a new connection every time a connection is requested. Unlike DBCP ’s BasicDataSource , the connections provided by DriverManagerDataSource aren’t pooled.
     SimpleDriverDataSource —Works much the same as DriverManagerDataSource except that it works with the JDBC driver directly to overcome class loading issues that may arise in certain environments, such as in an OSG i container.
     SingleConnectionDataSource —Returns the same connection every time a connection is requested. Although SingleConnectionDataSource isn’t exactly a pooled data source, you can think of it as a data source with a pool of exactly
    one connection.

    (1)java方式

    1 @Bean
    2 public DataSource dataSource() {
    3     DriverManagerDataSource ds = new DriverManagerDataSource();
    4     ds.setDriverClassName("org.h2.Driver");
    5     ds.setUrl("jdbc:h2:tcp://localhost/~/spitter");
    6     ds.setUsername("sa");
    7     ds.setPassword("");
    8     return ds;
    9 }

    (2)xml方式

    1 <bean id="dataSource"
    2   class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    3   p:driverClassName="org.h2.Driver"
    4   p:url="jdbc:h2:tcp://localhost/~/spitter"
    5   p:username="sa"
    6   p:password="" />

    The only significant difference with these data-source beans as compared to the pooling data-source beans is that because they don’t provide a connection pool, there are no pool configuration properties to set.

    4.Using an embedded data source(适合测试用)

    Although it’s not very useful in production settings, an embedded database is a perfect choice for development and testing purposes. That’s because it allows you to populate your database with test data that’s reset every time you restart your application or run your tests.

    (1)java方式

    1 @Bean
    2 public DataSource dataSource() {
    3     return new EmbeddedDatabaseBuilder()
    4         .setType(EmbeddedDatabaseType.H2)
    5         .addScript("classpath:schema.sql")
    6         .addScript("classpath:test-data.sql")
    7         .build();
    8 }

    (2)xml方式

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/jdbc
     3 http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
     4 http://www.springframework.org/schema/beans
     5 http://www.springframework.org/schema/beans/spring-beans.xsd">
     6     ...
     7     <jdbc:embedded- database id="dataSource" type="H2">
     8         <jdbc:script location="com/habuma/spitter/db/jdbc/schema.sql" />
     9         <jdbc:script location="com/habuma/spitter/db/jdbc/test-data.sql" /> 
    10     </jdbc:embedded-database>
    11 
    12         ...
    13 </beans>

    5. Using profiles to select a data source

    生产、测试等不同环境要用不同的数据源,可用profile实现自动切换

    (1)java方式

     1 package com.habuma.spittr.config;
     2 import org.apache.commons.dbcp.BasicDataSource;
     3 import javax.sql.DataSource;
     4 import org.springframework.context.annotation.Bean;
     5 import org.springframework.context.annotation.Configuration;
     6 import org.springframework.context.annotation.Profile;
     7 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
     8 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
     9 import org.springframework.jndi.JndiObjectFactoryBean;
    10 @Configuration
    11 public class DataSourceConfiguration {
    12     @Profile("development")
    13     @Bean
    14     public DataSource embeddedDataSource() {
    15         return new EmbeddedDatabaseBuilder()
    16             .setType(EmbeddedDatabaseType.H2)
    17             .addScript("classpath:schema.sql")
    18             .addScript("classpath:test-data.sql")
    19             .build();
    20     }
    21     @Profile("qa")
    22     @Bean
    23     public DataSource Data() {
    24         BasicDataSource ds = new BasicDataSource();
    25         ds.setDriverClassName("org.h2.Driver");
    26         ds.setUrl("jdbc:h2:tcp://localhost/~/spitter");
    27         ds.setUsername("sa");
    28         ds.setPassword("");
    29         ds.setInitialSize(5);
    30         ds.setMaxActive(10);
    31         return ds;
    32     }
    33     @Profile("production")
    34     @Bean
    35     public DataSource dataSource() {
    36         JndiObjectFactoryBean jndiObjectFactoryBean
    37             = new JndiObjectFactoryBean();
    38         jndiObjectFactoryBean.setJndiName("jdbc/SpittrDS");
    39         jndiObjectFactoryBean.setResourceRef(true);
    40         jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
    41         return (DataSource) jndiObjectFactoryBean.getObject();
    42     }
    43 }

    (2)xml方式

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/jdbc
     3 http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
     4 http://www.springframework.org/schema/jee
     5 http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
     6 http://www.springframework.org/schema/beans
     7 http://www.springframework.org/schema/beans/spring-beans.xsd">
     8     <beans profile="development">
     9         <jdbc:embedded- database id="dataSource" type="H2">
    10             <jdbc:script location="com/habuma/spitter/db/jdbc/schema.sql" />
    11             <jdbc:script location="com/habuma/spitter/db/jdbc/test-data.sql" /> 
    12         </jdbc:embedded- database>
    13     </beans>
    14     <beans profile="qa">
    15         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    16             p:driverClassName="org.h2.Driver" 
    17             p:url="jdbc:h2:tcp://localhost/~/spitter" 
    18             p:username="sa" p:password="" 
    19             p:initialSize="5" 
    20             p:maxActive="10" /> 
    21     </beans>
    22     <beans profile="production">
    23         <jee:jndi-lookup id="dataSource" jndi-name="/jdbc/SpitterDS" resource-ref="true" /> 
    24     </beans>
    25 </beans>

     

  • 相关阅读:
    设计模式:访问者模式
    设计模式:模板模式
    三分法——求解凸性函数的极值问题——czyuan原创
    素数&&Miller_Rabbin
    【算法入门】深度优先搜索(DFS)
    快速幂取模
    hrbeu1280Turn the corner
    hdoj_3400Line belt
    【专题】三分法和牛顿迭代法总结
    zoj_3203Light Bulb
  • 原文地址:https://www.cnblogs.com/shamgod/p/5342324.html
Copyright © 2020-2023  润新知