Obtain resource _ SpringJNDI + Mysql + Tomcat from the server container through JNDI

Source: Internet
Author: User
Get resource _ SpringJNDI + Mysql + Tomcat from the server container through JNDI get DataSource resource from the server container through JNDI (managed by the container, do not close it, the container will handle it) in the previous article, we used dbcp. Here we use JNDI: connect data using JNDI: Comment beanid1_ceclassorg in Spring. a

Obtain resources from the server container through JNDI _ Spring JNDI + Mysql + Tomcat get DataSource resources from the server container through JNDI (managed by the container, do not close it, the container will handle it by itself) in the previous article, we used dbcp. Here we use JNDI: connect data using JNDI: Comment bean id = dataSource class = org in Spring. a

Obtain resource _ Spring JNDI + Mysql + Tomcat from the server container through JNDI
Get DataSource resources from the server container through JNDI (managed by the container, do not close it, the container will handle it by itself) in the previous article we used dbcp, here we use JNDI:
Connect data using JNDI:

Comment in Spring

 
  
  
  
  
 

Usage:

 
 
  
   
    
Java: comp/env/jdbc/joba
   
  
 

1. Add a database driver File

Access the database through the data source. Because the data source is created and maintained by tomcat, you must copy the MySql driver package to the Tomcat root directory \ lib.

2. Configure the data source
In the tomcat root directory \ conf \ context. xml Add the following configurations to the node:

 
Here we will introduce Node attributes:
(1) name: Specifies the Resource's JNDI name, which can be customized
(2) auth: Specifies the Manager for Resource management. It has two optional values: Container and Application. Container indicates that resources are created and managed by containers, and Application indicates that resources are created and managed by web applications.
(3) type: Specifies the java class name to which the Resource belongs.
(4) maxActive: specifies the maximum number of active database connections in the database connection pool. The value is 0, indicating no restriction.
(5) maxIdle: specifies the maximum number of idle database connections in the database connection pool. The value is 0, indicating no restriction.
(6) maxWait: specifies the maximum time (in milliseconds) for idle database connections in the database connection pool. If this time is exceeded, an exception is thrown. The value is-1, indicating unlimited waiting.
(7) username: Specifies the username used to connect to the database.
(8) password: password used to connect to the database
(9) driverClassName: Specifies the JDBC driver to connect to the database.
(10) url: Specify the url to connect to the database

Test: It must be run in the tomcat container. To start tomcat, junit cannot be used.

Here I use the test of my practice project. When I add the following code to the login method of action, the code will be executed: This code was tested by testAdd below.

package com.jboa.service;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.jboa.model.Department;import com.jboa.model.Employee;import com.jboa.model.Postion;public class EmployeeServiceTest {@Testpublic void testAdd() {ApplicationContext ac = new ClassPathXmlApplicationContext("/*ApplicationContext.xml");EmployeeService employeeService = (EmployeeService) ac.getBean("employeeService");Employee employee = new Employee();employee.setSn("user11111112");employee.setPassword("user11111112");employee.setStatus("1");employee.setName("user1111112");Postion p = new Postion();p.setId(2);employee.setPostion(p);Department d = new Department();d.setId(1);employee.setDepartment(d);employeeService.add(employee);}}
Added successfully. The test is successful;

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.