Spring中获得dataSurce对象的方法

转载地址:http://www.oschina.net/code/snippet_117958_4699


在Spring框架中有如下3种获得DataSource对象的方法: 

1.从JNDI获得DataSource. 
2.从第三方的连接池获得DataSource. 

3.使用DriverManagerDataSource获得DataSource.

代码片段(3)

[代码] JNDI方式

view source
print ?
01 1、SpringJNDI数据源配置信息:
02   "dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
03    "jndiName">
04     java:comp/env/jcptDataSourceJNDI
05    
06   
07   jcptDataSourceJNDI是tomcat或者其他应用服务器配置的JNDI.
08  
09 2、关于JNDI的配置(tomcat):
10   修改tomcat目录conf/context.xml文件:
11   "jcptDataSourceJNDI" auth="Container"type="javax.sql.DataSource"
12       maxActive="100" maxIdle="30" maxWait="10"   username="tysp"
13       password="12345678" driverClassName="oracle.jdbc.driver.OracleDriver"
14       url="jdbc:oracle:thin:@192.168.1.35:1521:orcl"/> 
15  
16 3、通过JNDI获取DataSource:
17   Context context = new InitialContext();
18   DataSource ds = (DataSource)context.lookup("java:comp/env/jcptDataSourceJNDI");

[代码] 使用DBCP连接池获取

01 要在Spring中使用DBCP连接池,需要引入commons-collections.jar、commons-dbcp.jar和commons-pool.jar。
02  
03 "dataSource" class="org.apache.commons.dbcp.BasicDataSource">
04         "driverClassName"value="oracle.jdbc.driver.OracleDriver">
05         "url" value="jdbc:oracle:thin:@192.168.1.35:1521:orcl">
06         "username" value="or_meal">
07         "password" value="or_meal">
08         "maxActive" value="100">
09         "maxIdle" value="30">
10         "maxWait" value="10">
11         "defaultAutoCommit" value="false">
12     
13  
14     "sessionFactory"
15         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
16         "dataSource">
17             "dataSource" />
18         
19  
20         "hibernateProperties">
21             
22                 "hibernate.dialect">org.hibernate.dialect.Oracle9Dialect
23                 
24                 "show_sql">true
25                 "format_sql">true
26             
27         
28         "mappingResources">
29         
30         
31     

[代码] 使用DriverManagerDataSource

01 "dataSource"
02         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
03         "driverClassName">
04             oracle.jdbc.driver.OracleDriver
05             
06         
07         "url">
08             jdbc:oracle:thin:@192.168.1.35:orcl
09             
10         
11         "username">
12             or_meal
13         
14         "password">
15             or_meal
16         
17     

你可能感兴趣的:(Java,JAVA,数据库,ORACLE,sql)