How to configure the DataSource data source in spring

Source: Internet
Author: User
Tags auth connect resource tomcat

There are 3 ways to get DataSource objects in the Spring framework:

1. Obtain DataSource from Jndi.

2. Obtain DataSource from a third party connection pool.

3. Use Drivermanagerdatasource to obtain datasource.

First, get DataSource from Jndi

Springjndi Data Source configuration information:

<bean id= "DataSource" class= "Org.springframework.jndi.JndiObjectFactoryBean" >  
<property name= " Jndiname ">  
 <value>java:comp/env/jcptDataSourceJNDI</value>  
</property>  
 </ Bean>

Jcptdatasourcejndi is a jndi for Tomcat or other application server configuration.

2. About Jndi configuration (tomcat):

To modify the Tomcat directory Conf/context.xml file or Server.xml file:

<resource name= "Jcptdatasourcejndi" auth= "Container" type= "Javax.sql.DataSource"
maxactive=
" maxidle= "maxwait=" "
username=" TYSP
"password=" 12345678 "Driverclassname=" Oracle.jdbc.driver.OracleDriver "
url=" JDBC:ORACLE:THIN:@192.168.1.35:1521:ORCL "
  />

Add the <Resource> element:<resource> element to the Server.xml to define the Jndi Resource.

Properties: Describing

Name: Specifies the Jndi name of the resource

Auth: Specifies the manager for managing resource, which has two optional values: Container, Application

Type: Specifies the Java class name to which resource belongs

<Resource> element Add <ResourceParams> element:<resourceparams> element is used to specify a variety of parameter values (can also be configured as above)

Properties: Describing

FACTORY Specifies the factory class name of the generated Dataresource

maxactive Specifies the maximum number of connections active in the database connection pool, 0 means unrestricted

MAXIDLE Specifies the maximum number of connections that are idle in the database connection pool, 0 means unrestricted

MAXWAIT Specifies the maximum amount of time that a connection is idle in the connection pool, exceeding throws an exception,-1 indicates an infinite

Username Specify the user name to connect to the database

Password specify the password to connect to the database

Driverclassname Specify the JDBC driver to connect to the database

URL specifies the URL of the connection database

3. Get DataSource through Jndi:

Context context = new InitialContext ();

DataSource ds = (DataSource) context.lookup ("Java:comp/env/jcptdatasourcejndi");

4. Add in Web.xml (can not be used in spring configuration)

<resource-ref>   
 <description>db connection</description>   
 <res-ref-name>jdbc/ testdb</res-ref-name>   
 <res-type>javax.sql.DataSource</res-type>   
 <res-auth> Container</res-auth>   
</resource-ref>

Add <resource-ref> element:<resource-ref> elements in Web.xml to reference Jndi resources in Web applications

Properties: Describing

Description A description of the referenced resource

RES-REF-NAME Specifies the Jndi name of the referenced resource, corresponding to the name attribute in the <Resource> element

RES-TYPE specifies the class name of the referenced resource, corresponding to the type attribute in the <Resource> element

Res-auth Specifies the manager of the referenced resource, corresponding to the Auth attribute in the <Resource> element

For more information: refer to http://blog.csdn.net/cyxlzzs/article/details/7352837

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.