Solution to the JNDI datasource involved when running tomcat in the WEB Project Development Environment

Source: Internet
Author: User

Usually in J2EE development, datasource configured on the server through JNDI is used, for example, the final application runs on WebLogic, while Tomcat may be used on the local machine in the development environment, at this time, we need to configure a datasource on Tomcat. on the Internet, we can see that many of the solutions are to change the server in the Tomcat runtime environment. XML or context. XML, in fact, the simplest way is to add a context in the project's META-INF directory. in this way, you do not need to change the tomcat configuration on your machine for every member of the project team. For example:

The content of context is as follows:

<?xml version="1.0" encoding="UTF-8"?><Context><Resource name="xxxDS" auth="Container" type="javax.sql.DataSource"maxActive="20" maxIdle="30" maxWait="10000" username="xx" password="xx"driverClassName="oracle.jdbc.OracleDriver"url="jdbc:oracle:thin:@147.151.240.xx:1521:orcl" /></Context>

In this case, the following Java code accesses the JNDI to obtain the datasource. In the eclipse development environment, Tomcat can be integrated and finally released to WebLogic for running.

private void testJNDIDataSource(){try {InitialContext context = new InitialContext();javax.sql.DataSource ds = (javax.sql.DataSource) context.lookup ("java:/comp/env/xxxDS");System.out.println(ds);} catch (Exception e) {e.printStackTrace();};}

If it is integrated with spring, you do not need to configure the context. xml method. The following configuration is used to solve the problem. However, this introduces the configuration that should not be done in the production environment to adapt to the test environment. It is better to use this method.

<bean id="dataSource"        class="org.springframework.jndi.JndiObjectFactoryBean">        <property name="jndiName">            <value>xxxDS</value>        </property>    <property name="defaultObject" ref="dataSource1"/>     </bean>    <bean id="dataSource1"    class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />    <property name="url" value="jdbc:oracle:thin:@147.151.240.xx:1521:orcl" />    <property name="username" value="xxx" />    <property name="password" value="xxx" /></bean>

The profile configuration was introduced in spring3.1 to use different bean configurations in different environments.

<beans profile="weblogic"><bean id="dataSource"    class="org.springframework.jndi.JndiObjectFactoryBean">    <property name="jndiName">    <value>xxxDS</value>    </property><property name="resourceRef" value="true"/>     </bean> </beans><beans profile="tomcat"><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /><property name="url" value="jdbc:oracle:thin:@147.151.240.89:1521:orcl" /><property name="username" value="but" /><property name="password" value="but" /></bean></beans>

In web. XML, specify the profile to be enabled.

<context-param>    <param-name>spring.profiles.active</param-name>    <param-value>tomcat</param-value></context-param>

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.