Spring中獲得dataSurce物件的方法

舟之橋發表於2013-03-20

轉載地址:http://www.oschina.net/code/snippet_117958_4699


在Spring框架中有如下3種獲得DataSource物件的方法: 

1.從JNDI獲得DataSource. 
2.從第三方的連線池獲得DataSource. 

3.使用DriverManagerDataSource獲得DataSource.

程式碼片段(3)

[程式碼] JNDI方式

01 1、SpringJNDI資料來源配置資訊:
02   <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
03    <property name="jndiName">
04     <value>java:comp/env/jcptDataSourceJNDI</value>
05    </property>
06   </bean>
07   jcptDataSourceJNDI是tomcat或者其他應用伺服器配置的JNDI.
08  
09 2、關於JNDI的配置(tomcat):
10   修改tomcat目錄conf/context.xml檔案:
11   <Resource name="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 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
04         <property name="driverClassName"value="oracle.jdbc.driver.OracleDriver"></property>
05         <property name="url" value="jdbc:oracle:thin:@192.168.1.35:1521:orcl"></property>
06         <property name="username" value="or_meal"></property>
07         <property name="password" value="or_meal"></property>
08         <property name="maxActive" value="100"></property>
09         <property name="maxIdle" value="30"></property>
10         <property name="maxWait" value="10"></property>
11         <property name="defaultAutoCommit" value="false"></property>
12     </bean>
13  
14     <bean id="sessionFactory"
15         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
16         <property name="dataSource">
17             <ref bean="dataSource" />
18         </property>
19  
20         <property name="hibernateProperties">
21             <props>
22                 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect
23                 </prop>
24                 <prop key="show_sql">true</prop>
25                 <prop key="format_sql">true</prop>
26             </props>
27         </property>
28         <property name="mappingResources">
29         <list></list>
30         </property>
31     </bean>

[程式碼] 使用DriverManagerDataSource

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

相關文章