Tomcat配置JNDI数据源

环境:

Tomcat7

摘要说明:

本篇文章主要讲述如何在tomcat里配置JNDI数据源,以及web项目如何使用JNDI数据源;

步骤:

1.Tomcat中配置JNDI数据源

在tomcat的config/content.xml中配置JNDI数据源:

<Resource name="xxx" auth="Container" type="javax.sql.DataSource"
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx"
		username="xxx" password="xxx" maxActive="50"
		maxIdle="30" maxWait="10000" />

name:数据源名称,多个时需唯一

auth:默认"Container",不需变更

type:指定数据源类型,默认为"javax.sql.DataSource"

driverClassName:指定jdbc的jar包:mysql为com.mysql.jdbc.Driver、oracle为oracle.jdbc.driver.OracleDriver、sqlserver为com.microsoft.sqlserver.jdbc.SQLServerDriver、其他也可以;

url:指定数据库连接地址、各数据源格式不同;

username:数据库用户名

password:数据库用户密码

maxActive:表示最大连接数 

maxIdle:表示最小连接数

maxWait:最大等待时间(毫秒)

2.web.xml配置JNDI数据源

在项目中的web.xml中直接配置数据源,description和res-ref-name需与上述配置一致:

<resource-ref>
	<description>xxx</description>
	<res-ref-name>xxx</res-ref-name>
	<res-type>javax.sql.DataSource</res-type>
	<res-auth>Container</res-auth>
</resource-ref> 

3.使用spring配置JNDI数据源

若项目中使用了spring,则不需要在web.xml中使用;

使用org.springframework.jndi.JndiObjectFactoryBean配置JNDI数据源,jndiname的格式为java:comp/env/JNDI数据源名称;

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

猜你喜欢

转载自blog.csdn.net/u010904188/article/details/80969199