NutzCN Logo
精华 tomcat7下 jndi连接池的问题
发布于 2978天前 作者 ericleung 3511 次浏览 复制 上一个帖子 下一个帖子
标签:

在tomcat conf 下的 server.xml 中的

<Resource name="jdbc/egp"
          auth="Container"
          type="javax.sql.DataSource"
          factory="com.alibaba.druid.pool.DruidDataSourceFactory"
          testWhileIdle="true"
          testOnBorrow="true"
          testOnReturn="false"
          validationQuery="SELECT 1"
          validationInterval="30000"
          timeBetweenEvictionRunsMillis="30000"
          maxActive="100"
          minIdle="10"
          maxWait="10000"
          initialSize="10"
          removeAbandonedTimeout="60"
          removeAbandoned="true"
          logAbandoned="true"
          minEvictableIdleTimeMillis="30000"
          username="root"
          password=""
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/egp?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull" />

druid,mysql,的jar包已入到 tomcat/lib下

Tomcat中配置了Realm 使用上面配置的连接池,登录正常,证明OK连接池OK了

<Realm className="org.apache.catalina.realm.DataSourceRealm"
		dataSourceName="jdbc/egp" userTable="v_users" userNameCol="user_name"
		userCredCol="user_pass" userRoleTable="v_roles" roleNameCol="role_name" />

在nutz IOC中配置 ,jdbc/egp 或 java:/comp/env/jdbc/egp 都有试过

{
  "defaultDao": {
    "type": "org.nutz.dao.impl.NutDao",
    "args": [{"jndi":"jdbc/egp"}]
  }
}

调用时报错,
驱动没有载入到?那Realm怎么又OK 的,有请兽叔帮忙看看。。

Caused by: org.nutz.lang.born.BorningException: Fail to born 'org.nutz.dao.impl.NutDao'
 by args: [
  @(org.apache.tomcat.dbcp.dbcp.BasicDataSource@25685c0e)] becasue:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:19)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
	... 34 more
Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
	at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
	at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
	at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
	at org.nutz.trans.Trans.getConnectionAuto(Trans.java:263)
	at org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:106)
	at org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:190)
	at org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:181)
	at org.nutz.dao.impl.NutDao.<init>(NutDao.java:113)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17)
	... 36 more
Caused by: java.sql.SQLException: No suitable driver
	at java.sql.DriverManager.getDriver(DriverManager.java:315)
	at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
	... 48 more
19 回复

报错的是org.apache.tomcat.dbcp.dbcp.BasicDataSource

但贴的配置是DruidDataSource

这是奇怪点之一,我根本就没有用过DBCP,但是把JAR包删了又报错。

我想知道的是,为什么ioc实例dao时会找到org.apache.tomcat.dbcp.dbcp.BasicDataSource 而不是javax.sql.DataSource

把jndi名称改成jdbc/dataSource

改了还是一样啊亲,版本是1.r.58,druid-1.0.13

把tomcat连接池的jar干掉, 然后启动,贴报错信息

通过jndi获取DataSource的时候, tomcat容器返回啥就是啥,并不是nutz故意去加载BasicDataSource

tomcat启动时没有报,调用时报了,找不到类

org.nutz.ioc.IocException: [userModule, defaultDao] # FAIL to create Ioc Bean name=[defaultDao]
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:203)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]
	at org.nutz.lang.Lang.wrapThrow(Lang.java:182)
	at org.nutz.ioc.val.JNDI_Value.get(JNDI_Value.java:40)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:75)
	... 34 more
Caused by: javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]
	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:119)
	at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:842)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:830)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:167)
	at org.nutz.ioc.val.JNDI_Value.get(JNDI_Value.java:37)
	... 35 more
Caused by: java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:115)
	... 41 more

<Resource name="jdbc/dataSource"
		  auth="Container"
		  type="javax.sql.DataSource"
		  factory="com.alibaba.druid.pool.DruidDataSourceFactory"
		  testWhileIdle="true"
		  testOnBorrow="true"
		  testOnReturn="false"
		  validationQuery="SELECT 1"
		  validationInterval="30000"
		  timeBetweenEvictionRunsMillis="30000"
		  maxActive="100"
		  minIdle="10"
		  maxWait="10000"
		  initialSize="10"
		  removeAbandonedTimeout="60"
		  removeAbandoned="true"
		  logAbandoned="true"
		  minEvictableIdleTimeMillis="30000"
		  username="root"
		  password=""
		  driverClassName="com.mysql.jdbc.Driver"
		  url="jdbc:mysql://localhost:3306/egp?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull" />
  "defaultDao": {
    "type": "org.nutz.dao.impl.NutDao",
    "args": [{"jndi":"jdbc/dataSource"}]
  },

org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:115)

ResourceFactory

再是一种情况:

把tomcat的连接池加回去, 然后driud的jar干掉, 贴报错信息

ResourceFactory, 68行,这里是引用dbcp的原因了。

if(ref.getClassName().equals("javax.sql.DataSource")) {
          javaxMailSessionFactoryClassName = System.getProperty("javax.sql.DataSource.Factory", "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory");

          try {
            factory = (ObjectFactory)Class.forName(javaxMailSessionFactoryClassName).newInstance();
          } catch (Exception var14) {
            ex2 = new NamingException("Could not create resource factory instance");
            ex2.initCause(var14);
            throw ex2;
          }
        }

简直天坑啊....

按你所说的,这就回到之前所说的,找不到classname名的问题。

org.nutz.ioc.IocException: [userModule, defaultDao] # FAIL to create Ioc Bean name=[defaultDao]
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:203)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.nutz.lang.born.BorningException: Fail to born 'org.nutz.dao.impl.NutDao'
 by args: [
  @(org.apache.tomcat.dbcp.dbcp.BasicDataSource@412107b1)] becasue:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:19)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
	... 34 more
Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
	at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
	at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
	at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
	at org.nutz.trans.Trans.getConnectionAuto(Trans.java:263)
	at org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:106)
	at org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:190)
	at org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:181)
	at org.nutz.dao.impl.NutDao.<init>(NutDao.java:113)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17)
	... 36 more
Caused by: java.sql.SQLException: No suitable driver
	at java.sql.DriverManager.getDriver(DriverManager.java:315)
	at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
	... 48 more

@wendal 这样就永远用不了druid了。。。晕。。

不是可以设置环境变量吗? env.bat或env.sh里面加

JAVA_OPTS="-Djavax.sql.DataSource.Factory=com.alibaba.druid.pool.DruidDataSourceFactory"

我试了一下8.5.6,正常,不需要额外修改和配置

dao.js配置片段

		dao : {
			type : "org.nutz.dao.impl.NutDao",
			args : [{jndi:"jdbc/DataSource"}],
			fields : {
				//executor : {refer: "cacheExecutor"},
				runner : {refer: "daoRunner"},
				interceptors : [{refer:"cacheExecutor"}, "log", "time"]
			}
		},

tomcat的context.xml配置

<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
	
	  <Resource
      name="jdbc/DataSource"
      factory="com.alibaba.druid.pool.DruidDataSourceFactory"
      auth="Container"
      type="javax.sql.DataSource"

      maxActive="100"
      maxIdle="30"
      maxWait="10000"
      url="jdbc:mysql://127.0.0.1/nutzbook"
	  username="root"
	  password="root"
      />
</Context>

解决了,解决了,终于解决了。
本来配置在context.xml下还是没问题的。但是。。。
使用了 org.apache.catalina.realm.DataSourceRealm 的原因,连接池必须配置在tomcat server.xml下的GlobalNamingResources位置
在兽叔的指点下,在context.xml加入了,其实这个问题算是低级的,也不怪我,因为从来就没有写过ResourceLink。。。。。

添加回复
请先登陆
回到顶部