Spring configuration JNDI (connection pool)

 

 

1. Find the context.xml file in the conf folder of the Tomcat6.0 installation directory,
then open the context.xml, and add the following content between the tags <context></<context>: (For easy understanding, I made the configuration more detailed notes)


<Resource  name="jdbc/books"  
     auth="Container"      
     type="javax.sql.DataSource"      
     driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"      
     url="jdbc:sqlserver://localhost:1433;DatabaseName=books"      
     username="sa"      
     password="accp"      
     maxActive="100"      
     maxIdle="30"      
     maxWait="10000" />

Note:
1) name: specifies the JNDI name of the Resource
2) auth : specifies the Manager that manages the Resource
(Container: created and managed by the container | Application: created and managed by the Web application)
3) type: specifies the Java class to which the Resource belongs4
) maxActive: specifies the maximum number of database connections that are active in the connection pool
5) maxIdle: specifies the maximum number of database connections that are idle in the
connection pool 6) maxWait: specifies the maximum time that a connection in the connection pool is idle
7) username: database username
8) password: database password
9) driverClassName: database connection driver class name
10) url: database connection string

 

--JDBC获取
Context it = new  InitialContext();
 DataSource ds = (DataSource) it.lookup("java:comp/env/jdbc/books");
 Connection conn = ds.getConnection();

 

--Spring configuration

<bean id="dataSource"
  class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName"
   value="java:comp/env/jdbc/books">
  </property>
 </bean>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326959233&siteId=291194637