如何解决容器外调用jndi数据源
1个回答
展开全部
1.相应的数据库jar包 放tomcat的lib里
2.在tomcat中的context.xml文件中配置数据源参数如(db2数据库):
<GlobalNamingResources>
<Resource auth ="Container" description="User database that can be updated and saved" factory= "org.apache.catalina.users.MemoryUserDatabaseFactory" name= "UserDatabase" pathname ="conf/tomcat-users.xml" type= "org.apache.catalina.UserDatabase" />
<Resource auth ="Container" driverClassName="com.ibm.db2.jcc.DB2Driver" factory= "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" maxActive= "20" maxIdel ="10" maxWait="1000" name= "xxx" password= "xxx" type ="javax.sql.DataSource" url= "xxxx" username ="xxx"/>
</GlobalNamingResources >
<ResourceLink name= "jdbc/xxx" global= "jdbc/xxx" type="javax.sql.DataSource" />
3.在项目配置中配置:JNDI名服务查找
<!-- JNDI数据源 -->
<jee:jndi-lookup id ="dataSource" jndi-name="jdbc/xxxx"
proxy-interface= "javax.sql.DataSource" lookup-on-startup="false" />
4.在配置中建立数据库操作对象与数据源的关系:
<bean id= "dalClient" class= "xxxxxxx" >
<property name ="resources" value= "classpath*:conf/sqlMap/sqlMap_*.xml" />
<!-- 定义默义的数据源 -->
<property name ="defaultDataSource" ref= "xxxxxx" />
</bean >
5.在web.xml中将3,4配置文件通过上下文加载到容器中
6.在代码中直接使用dalClient就可以了
2.在tomcat中的context.xml文件中配置数据源参数如(db2数据库):
<GlobalNamingResources>
<Resource auth ="Container" description="User database that can be updated and saved" factory= "org.apache.catalina.users.MemoryUserDatabaseFactory" name= "UserDatabase" pathname ="conf/tomcat-users.xml" type= "org.apache.catalina.UserDatabase" />
<Resource auth ="Container" driverClassName="com.ibm.db2.jcc.DB2Driver" factory= "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" maxActive= "20" maxIdel ="10" maxWait="1000" name= "xxx" password= "xxx" type ="javax.sql.DataSource" url= "xxxx" username ="xxx"/>
</GlobalNamingResources >
<ResourceLink name= "jdbc/xxx" global= "jdbc/xxx" type="javax.sql.DataSource" />
3.在项目配置中配置:JNDI名服务查找
<!-- JNDI数据源 -->
<jee:jndi-lookup id ="dataSource" jndi-name="jdbc/xxxx"
proxy-interface= "javax.sql.DataSource" lookup-on-startup="false" />
4.在配置中建立数据库操作对象与数据源的关系:
<bean id= "dalClient" class= "xxxxxxx" >
<property name ="resources" value= "classpath*:conf/sqlMap/sqlMap_*.xml" />
<!-- 定义默义的数据源 -->
<property name ="defaultDataSource" ref= "xxxxxx" />
</bean >
5.在web.xml中将3,4配置文件通过上下文加载到容器中
6.在代码中直接使用dalClient就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询