{"id":1948,"date":"2019-01-04T08:38:46","date_gmt":"2019-01-04T00:38:46","guid":{"rendered":"http:\/\/ufqi.com\/blog\/?p=1948"},"modified":"2019-01-04T08:39:57","modified_gmt":"2019-01-04T00:39:57","slug":"tomcat-jdbc-mysql-namingcontext","status":"publish","type":"post","link":"https:\/\/ufqi.com\/blog\/tomcat-jdbc-mysql-namingcontext\/","title":{"rendered":"Apache Tomcat JDBC\u8fde\u63a5\u6c60\u63a5\u5165MySQL\u6570\u636e\u5e93\u7684NamingContext\u5f02\u5e38\u6392\u89e3"},"content":{"rendered":"<p>\u5728\u6700\u8fd1\u65b0\u90e8\u7f72\u7684 <a href=\"https:\/\/ufqi.com\/naturedns\/search.php?q=-GWA2\">-GWA2<\/a> in Java \u7684\u5b9e\u4f8b\u4e2d\uff0c\u4e00\u5207\u8fdb\u5c55\u987a\u5229\uff0c\u552f\u72ec\u5728\u542f\u7528\u8fde\u63a5\u6c60\u8bbf\u95ee Tomcat \u63a5\u7ba1\u7684 MySQL \u6570\u636e\u5e93\u8fde\u63a5\u6c60\u65f6\u62a5\u9519\u51fa\u5f02\u5e38\u3002\u95ee\u9898\u6392\u89e3\u8fc7\u7a0b\u66f2\u6298\uff0c\u503c\u5f97\u8bb0\u5f55\u5982\u4e0b\u3002\u670d\u52a1\u5668\u7aef\u65e5\u5fd7\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<p>java.lang.ClassCastException: class org.apache.naming.NamingContext cannot be cast to class javax.sql.DataSource (org.apache.naming.NamingContext is in unnamed module of loader java.net.URLClassLoader @5383967b; javax.sql.DataSource is in module java.sql of loader &#8216;platform&#8217;)<\/p>\n<\/blockquote>\n<p>\u6839\u636e\u62a5\u9519\u4fe1\u606f\uff0c\u627e\u51fa\u62a5\u51fa\u95ee\u9898\u7684\u4ee3\u7801\u6bb5\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<p>if(this.hasSocketPool){<br \/> try{<br \/>\u00a0 \u00a0 this.ctx = new InitialContext();<br \/>\u00a0 \u00a0 this.ds = (DataSource)ctx.lookup(&#8220;java:comp\/env\/jdbc\/&#8221;+this.myDb); \/\/- error!<br \/> }<br \/> catch(Exception ex){<br \/> ex.printStackTrace();<br \/> }<br \/> }<\/p>\n<\/blockquote>\n<p>\u5c31\u662f\u5728 ctx.lookup \u8fd4\u56de\u5bf9\u8c61\u8fdb\u884c\u5f3a\u5236\u7c7b\u578b\uff08DataSource\uff09\u8f6c\u6362\u65f6\u53d1\u9001\u9519\u8bef\uff0c\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p>\u8ba9\u4eba\u8d39\u89e3\u7684\u662f\uff0c\u8fd9\u4e9b\u4ee3\u7801\u5728\u53e6\u5916\u7684\u8fd0\u884c\u65f6\u73af\u5883\u65f6\uff0c\u8fd0\u884c\u826f\u597d\uff0c\u96be\u9053\u8ddf\u65b0\u90e8\u7f72\u7684\u73af\u5883\u6709\u5173\uff1f Tomcat 9.0+ \/ MySql 8.0+ \/ Java 11.0+ ?<\/p>\n<p>\u62a5\u9519\u4fe1\u606f\u7684\u5b57\u9762\u610f\u601d\u662f \u201cA\u7c7b\u4e0d\u80fd\u5f3a\u5236\u8f6c\u4e3aB\u7c7b\uff08A\u7c7b\u5728\u672a\u547d\u540d\u7684\u52a0\u8f7d\u5668\u6a21\u5757C\u4e2d\uff0cB\u7c7b\u5728platform\u52a0\u8f7d\u5668\u7684java.sql\u6a21\u5757\u4e2d\uff09\u201d\u3002\u6839\u636e\u8fd9\u4e9b\u7406\u89e3\uff0c\u5927\u81f4\u662f\u52a0\u8f7d\u7684\u7c7b\u51fa\u73b0\u9519\u4e71\u4e86\uff0c\u4e8e\u662f\u6cbf\u7740\u8fd9\u4e2a\u601d\u8def\u641c\u7d22\u4e00\u4e0b\u7f51\u4e0a\u5176\u4ed6\u540c\u5b66\u7684\u8fc7\u5f80\u7ecf\u5386\u3002<\/p>\n<p>\u7f51\u4e0a\u7684\u8d44\u8baf\u5927\u81f4\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u7684\u786e\u52a0\u8f7d\u9519\u4e86\u00a0org.apache.naming.* \u76f8\u5173\u7684\u7c7b\uff0c\u53e6\u5916\u4e00\u79cd\u66f4\u5927\u7684\u53ef\u80fd\u662f\u6ca1\u6709\u6b63\u786e\u5730\u5728 Tomcat\u4e2d\u914d\u7f6e conf\/server.xml \u548c conf\/web.xml \uff0c\u8fd9\u4e2a\u53c2\u7167 Tomcat\u7684\u624b\u518c\u53ef\u4ee5\u5f88\u5feb\u6392\u9664\u3002<\/p>\n<p>\u800c\u7b2c\u4e00\u79cd\uff0c\u52a0\u8f7d\u9519\u4e71\u7684\u60c5\u51b5\uff0c\u5728GWA2 Java\u4e2d\uff0c\u7406\u8bba\u4e0a\u4e0d\u5e94\u8be5\u53d1\u751f\uff0c\u56e0\u4e3a\u6211\u4eec\u5728 inc\/MySql.class \u4e2d\u660e\u786e\u5730\u58f0\u660e\u4e86\u52a0\u8f7d\u5982\u4e0b\u7c7b:<\/p>\n<blockquote>\n<p>&lt;%@page import=&#8221;javax.sql.DataSource,<br \/>java.sql.DriverManager,<br \/>java.sql.ResultSet,<br \/>java.sql.PreparedStatement,<br \/>javax.naming.Context,<br \/>javax.naming.InitialContext&#8221;%&gt;<\/p>\n<\/blockquote>\n<p>\u663e\u7136\uff0c\u5176\u4e2d\u5e76\u6ca1\u6709\u663e\u793a\u5730\u52a0\u8f7d\u4efb\u4f55 org.apache.naming \u76f8\u5173\u7684\u7c7b\u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u6211\u4eec\u8fd8\u662f\u5728\u4ee3\u7801\u5c42\u9762\u5c06\u7c7b\u7684\u8c03\u7528\u518d\u4e00\u6b21\u7684\u660e\u786e\u4e0b\u6765\uff0c\u4f7f\u7528\u7c7b\u7684\u7edd\u5bf9\u8def\u5f84\u4ee5\u907f\u514d\u53ef\u80fd\u5f15\u8d77\u7684\u6df7\u6dc6\u3002\u6539\u8fdb\u540e\u7684\u4ee3\u7801\u4e3a\uff1a<\/p>\n<blockquote>\n<p>javax.naming.Context ctx = new javax.naming.InitialContext();<br \/> javax.naming.Context envCtx = (javax.naming.Context)ctx.lookup(&#8220;java:comp\/env&#8221;);<br \/> envCtx = (javax.naming.Context)envCtx;<br \/> Object dsObj = envCtx.lookup(&#8220;jdbc\/&#8221;+this.myDb);<\/p>\n<\/blockquote>\n<p>\u5f88\u9057\u61be\uff0c\u5e26\u4e0a\u7edd\u5bf9\u8def\u5f84\u7684\u7c7b\u7684\u58f0\u660e\u4e0e\u5e94\u7528\u4e8e\u4e8b\u65e0\u8865\u3002<\/p>\n<p>\u8feb\u4e0d\u5f97\u5df2\uff0c\u6211\u4eec\u51b3\u5b9a\u5c06\u90e8\u7f72\u5f52\u96f6\uff0c\u6e05\u7a7a\u6240\u6709\u914d\u7f6e\uff0c\u91cd\u65b0\u4e0b\u8f7d\u6700\u65b0\u7248\u7684 Tomcat, GWA2 Java\u7b49\uff0c\u7136\u540e\u91cd\u65b0\u5b89\u88c5\uff0c\u518d\u6b21\u8fd0\u884c\u65f6\uff0c\u5f88\u9057\u61be\uff0c\u95ee\u9898\u4f9d\u65e7\uff01<\/p>\n<p>\u51e0\u4e4e\u8981\u653e\u5f03\u4e86\uff0c\u8fd9\u79cd\u53ef\u80fd\u7531\u4e8e\u8f6f\u4ef6\u4e0d\u5339\u914d\u5bfc\u81f4\u7684\u95ee\u9898\uff0c\u5df2\u7ecf\u8d85\u51fa\u5e94\u7528\u5c42\u548c\u4ee3\u7801\u5c42\u7684\u89e3\u51b3\u80fd\u529b\u8303\u7574\u3002\u4e0d\u80fd\u653e\u5f03\uff0c\u8fd8\u6709\u6700\u540e\u4e00\u62db\u2014\u2014\u5206\u6790\u6e90\u4ee3\u7801\u2014\u2014\u8fd8\u6ca1\u4f7f\u7528\u3002\u4e8e\u662f\u8010\u4e0b\u5fc3\u6765\uff0c\u7ffb\u770b Tomcat \u7684API\u6587\u6863\u548c Java \u7684API \u6587\u6863\uff0c\u9010\u4e2a\u5bf9\u8c61\u67e5\u8be2\u3001\u5206\u6790\uff0c\u5e76\u5728\u7a0b\u5e8f\u4e2d\u9010\u4e2a\u6b65\u9aa4\u8fdb\u884c\u65ad\u70b9\u6d4b\u8bd5\u3002<\/p>\n<blockquote>\n<p>javax.naming.NamingEnumeration dsList = envCtx.list(&#8220;jdbc\/&#8221;);<br \/> debug(&#8220;dsObj:&#8221;+dsObj+&#8221; \/ &#8220;+dsObj.toString()+&#8221; ctx:&#8221;+ctx+&#8221; envctx:&#8221;+envCtx+&#8221; envctx.getEnv:&#8221;+envCtx.getEnvironment());<br \/> debug(&#8220;dsList:&#8221;+dsList+&#8221; mydb:&#8221;+this.myDb);<br \/> while(dsList.hasMore()){<br \/>\u00a0 \u00a0 debug(&#8220;dsList: next:&#8221;+dsList.next());<br \/> }<\/p>\n<\/blockquote>\n<p><b><a class=\"bigfont\" href=\"https:\/\/ufqi.com\/naturedns\/search?q=-R\/B2SR\" target=\"_blank\">-R\/B2SR<\/a>\u00a0<\/b><br \/>https:\/\/tomcat.apache.org\/tomcat-9.0-doc\/api\/index.html<\/p>\n<p><b><a class=\"bigfont\" href=\"https:\/\/ufqi.com\/naturedns\/search?q=-R\/72SS\" target=\"_blank\">-R\/72SS<\/a>\u00a0<\/b><br \/>https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.naming\/javax\/naming\/Context.html<\/p>\n<p>\u8fdb\u4e00\u6b65\u5730\u65e5\u5fd7\u8f93\u51fa\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u4e86\u86db\u4e1d\u9a6c\u8ff9\uff0c\u5728\u6d4b\u8bd5\u4ee3\u7801\u7684\u7b2c\u4e8c\u6b65\uff0c\u539f\u672c\u5e94\u8be5\u8fd4\u56de javax.naming.Context \u5bf9\u8c61\u7684\uff0c\u5374\u88ab Tomcat \u66ff\u6362\u4e3a org.apache.naming.NamingContext. \u6240\u4ee5\u540e\u5c31\u51e0\u4e4e\u5168\u90fd\u8dd1\u504f\u4e86\u3002<\/p>\n<blockquote>\n<p>Thu Jan 03 05:56:32 UTC 2019 dsObj:org.apache.naming.NamingContext@368631b7 \/ org.apache.naming.NamingContext@368631b7 ctx:javax.naming.InitialContext@4f52e2fc envctx:org.apache.naming.NamingContext@5b51d294 envctx.getEnv:{}<br \/>Thu Jan 03 05:56:32 UTC 2019 dsList:org.apache.naming.NamingContextEnumeration@152fa16f mydb:<br \/>Thu Jan 03 05:56:32 UTC 2019 dsList: next:userdb: org.apache.tomcat.dbcp.dbcp2.BasicDataSource<br \/>Thu Jan 03 05:56:32 UTC 2019 dsList: next:contentdb: org.apache.tomcat.dbcp.dbcp2.BasicDataSource<br \/>Thu Jan 03 05:56:32 UTC 2019 dsList: next:newsdb: org.apache.tomcat.dbcp.dbcp2.BasicDataSource<\/p>\n<\/blockquote>\n<p>\u5f53\u6211\u4eec\u8c03\u7528\u00a0org.apache.naming.NamingContext \u5bf9\u8c61\u7684\u65b9\u6cd5 .getEnvironment \u548c .list \u8fdb\u4e00\u6b65\u5730\u8f93\u51fa\u65f6\u53d1\u73b0\uff0c\u914d\u7f6e\u4fe1\u606f userdb, contentdb \u548c newsdb \u90fd\u5728\u3002 \u4e3a\u4f55\u8fd4\u56de\u662f\u4e00\u4e2a\u5305\u542b\u5217\u8868\u7684\u5bf9\u8c61NamingContext \u800c\u4e0d\u662f\u4e00\u4e2a DataSource\u7c7b\u578b\u7684\u6570\u636e\uff1f<\/p>\n<p>\u8c1c\u5e95\u63ed\u5f00\uff01\u5728\u5f53\u524d\u8fd0\u884c\u65f6\u73af\u5883\u4e2d\u5173\u952e\u53d8\u91cf this.myDb \u4e3a\u7a7a\uff0c\u5f53\u4f20\u5165\u4e00\u4e2a\u7a7a\u503c\u7684 \u8d44\u6e90\u6807\u8bc6\u7b26\uff08\u6570\u636e\u5e93\u8d44\u6e90\uff09\u65f6\uff0cctx.lookup \u4e0d\u662f\u62a5\u9519\u672a\u627e\u5230\uff0c\u800c\u662f\u8fd4\u56de\u4e86DataSource\u7684\u7236\u7c7b NamingContext\uff01<\/p>\n<blockquote>\n<p>(DataSource)ctx.lookup(&#8220;java:comp\/env\/jdbc\/&#8221;+this.myDb); \/\/- error if empty myDb<\/p>\n<\/blockquote>\n<p>\u4fee\u6b63\u65b9\u5f0f\uff1a<br \/>1\uff09\u5728 GWA2 Java\u4e2d\u5bf9 this.myDb \u505a\u517c\u5bb9\uff0c\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u4e0d\u542f\u7528\u9ed8\u8ba4\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u8fde\u63a5\uff1b <br \/>2\uff09\u5c1d\u8bd5\u9012\u4ea4 bug\u7ed9 Apache Tomcat \u5f00\u53d1\u7ec4\uff0c\u4fee\u6539\u62a5\u9519\u4fe1\u606f\u6216\u6539\u8fdb\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u51cf\u5c11\u5bf9\u7528\u6237\u53ef\u80fd\u4ea7\u751f\u8bef\u5bfc\u7684\u4fe1\u606f\u3002<br \/>3) \u5c1d\u8bd5\u56de\u590dstackoverflow\u4e0a\u7c7b\u4f3c\u60ac\u800c\u672a\u51b3\u7684\u4e00\u95ee\u9898\u3002<\/p>\n<p><strong>\u7ffb\u9605\u6e90\u4ee3\u7801\uff0c\u4ed4\u7ec6\u67e5\u770bAPI\u6587\u6863\u4f9d\u7136\u662f\u89e3\u51b3\u95ee\u9898\u7684\u7ec8\u6781\u5927\u6740\u5668\u4e4b\u4e00\u3002<\/strong><\/p>\n<p><img src=\"https:\/\/ufqi.com\/blog\/wp-content\/uploads\/2016\/09\/gwa2-logo-201606.v2.png\" \/><\/p>\n<p>-GWA2 \u662f\u201d\u901a\u7528\u7f51\u7edc\u5e94\u7528\u67b6\u6784( General Web Application Architeture )\u201d\uff0c\u57fa\u4e8e -GWA2 \u53ef\u4ee5\u8f7b\u4fbf\u6784\u5efa\u5404\u79cd\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f,<br \/>\u5305\u62ec\u590d\u6742\u7684\u5728\u7ebf\u8d2d\u7269\u5546\u57ce\u3001\u00a0\u65c5\u6e38\u4ea4\u6613\u5e73\u53f0\u3001\u793e\u7fa4\u6216\u8005\u793e\u4ea4\u7f51\u7ad9\u548c\u65b0\u95fb\u8d44\u8baf\u7f51\u7ad9\u7b49\uff0c<br \/>\u4e5f\u5305\u62ec\u5404\u79cd\u4f01\u4e8b\u4e1a\u5355\u4f4d\u7f51\u4e0a\u95e8\u6237\uff0c\u5728\u7ebf\u4ea4\u4e92\u53ca\u670d\u52a1\u4f5c\u4e1a\u7cfb\u7edf\u7b49.<br \/>\u8fd8\u53ef\u4ee5\u5305\u62ec\u4e3aNativeApp\u505a\u670d\u52a1\u5668\u7aef\u652f\u6301, \u751a\u81f3\u662fWebApp\u7684\u5168\u90e8.<br \/>-GWA2 \u662f\u4e3a\u6570\u4e0d\u591a\u7684\u652f\u6301\u8de8\u5f00\u53d1\u8bed\u8a00\u7684\u5e94\u7528\u6846\u67b6\uff0c\u76ee\u524d\u652f\u6301 -Java, -PHP, -Perl, -Aspx and -Python .<\/p>\n<p>-GWA2 is a \u201cGeneral Web Application Architecture\u201d and based on -GWA2 developers can easily build a variety of network applications,<br \/>including complex online shopping malls, travel trading platforms, community or social networking sites and news information sites, etc.<br \/>Also the applications include various online portals of enterprises and institutions, online interaction and service operations systems.<br \/>Moreover it contains server-side support for NativeApp, or even all of the WebApp.<br \/>-GWA2 is one of the web frameworks which provide cross-language support for -Java, -PHP, -Perl, -Aspx and -Python at present.<\/p>\n<p><strong>-GWA2 is E.A.S.Y\u00a0<\/strong><br \/><b>Easy Along, Swift Yield<br \/><\/b>\u8f7b\u677e\u542f\u52a8, \u5feb\u901f\u4ea7\u51fa.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6700\u8fd1\u65b0\u90e8\u7f72\u7684 -GWA2 in Java \u7684\u5b9e\u4f8b\u4e2d\uff0c\u4e00\u5207\u8fdb\u5c55\u987a\u5229\uff0c\u552f\u72ec\u5728\u542f\u7528\u8fde &hellip; <a href=\"https:\/\/ufqi.com\/blog\/tomcat-jdbc-mysql-namingcontext\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[42,6,2],"tags":[86,146,255,229,271],"_links":{"self":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/1948"}],"collection":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/comments?post=1948"}],"version-history":[{"count":7,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/1948\/revisions"}],"predecessor-version":[{"id":1955,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/1948\/revisions\/1955"}],"wp:attachment":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/media?parent=1948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/categories?post=1948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/tags?post=1948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}