我正在使用 Eclipse 创建一个使用 JSP 页面的简单项目,该页面通过 JBoss 6 AS 从 MySQL 数据库中提取数据。看来我已将 MySQL 数据库正确绑定到 JBoss,因为它启动时没有错误,甚至显示以下消息。
17:09:43,782 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-6) JBAS010400: Bound data source [java:jboss/datasources/MyDatabase]
但是,当我尝试启动我的 index.jsp 页面时,我收到以下错误:
exception
javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get
connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for
jdbc/MyDatabase"
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.index_jsp._jspService(index_jsp.java:112)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
root cause
javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/MyDatabase"
org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.getConnection(QueryTagSupport.java:318)
org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.doStartTag(QueryTagSupport.java:201)
org.apache.jsp.index_jsp._jspx_meth_sql_005fquery_005f0(index_jsp.java:133)
org.apache.jsp.index_jsp._jspService(index_jsp.java:64)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
我似乎无法弄清楚为什么整个事情的最后一步,让我的 JSP 页面能够从数据库中读取是行不通的。
参考文件:
JBoss Standalone.xml
<subsystem xmlns="urn:jboss:domain:datasources:1.1">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jta="true" jndi-name="java:jboss/datasources/MyDatabase" pool-name="MyDatabase" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/MyDatabase</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>root</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
Module.xml 文件位于 JBOSS_HOME/modules/com/mysql/main
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.25-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
在 JBOSS_HOME/modules/com/mysql/main 我有 mysql-connector-java-5.1.25-bin.jar
Web.xml 内容
<display-name>TicketApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>response.jsp</welcome-file>
</welcome-file-list>
Jboss-Web.xml 内容
Blank for now
似乎当我将资源引用标签添加到 web.xml 和 jboss-web.xml 文件时,一切都崩溃了,我从 JBoss 收到以下错误:
17:18:29,519 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "MyApp.war"
17:18:29,994 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-11) MSC000001: Failed to start service jboss.deployment.unit."MyApp.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."MyApp.war".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of deployment "MyApp.war"
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:123) [jboss-as-server-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA-redhat-2.jar:1.0.2.GA-redhat-2]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA-redhat-2.jar:1.0.2.GA-redhat-2]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_21]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_21]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_21]
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011033: Could not load javax.sql.Datasource referenced in env-entry
at org.jboss.as.ee.component.deployers.ResourceReferenceProcessor.getResourceRefEntries(ResourceReferenceProcessor.java:159)
at org.jboss.as.ee.component.deployers.ResourceReferenceProcessor.processDescriptorEntries(ResourceReferenceProcessor.java:73)
at org.jboss.as.ee.component.deployers.AbstractDeploymentDescriptorBindingsProcessor.deploy(AbstractDeploymentDescriptorBindingsProcessor.java:100)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:116) [jboss-as-server-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
... 5 more
Caused by: java.lang.ClassNotFoundException: javax.sql.Datasource from [Module "deployment.TicketApp.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190) [jboss-modules.jar:1.1.3.GA-redhat-1]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468) [jboss-modules.jar:1.1.3.GA-redhat-1]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456) [jboss-modules.jar:1.1.3.GA-redhat-1]
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398) [jboss-modules.jar:1.1.3.GA-redhat-1]
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120) [jboss-modules.jar:1.1.3.GA-redhat-1]
at org.jboss.as.ee.component.deployers.ResourceReferenceProcessor.getResourceRefEntries(ResourceReferenceProcessor.java:157)
... 8 more
17:18:30,009 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS015870: Deploy of deployment "MyApp.war" was rolled back with failure message {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"MyApp.war\".POST_MODULE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"MyApp.war\".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of deployment \"MyApp.war\"
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011033: Could not load javax.sql.Datasource referenced in env-entry
Caused by: java.lang.ClassNotFoundException: javax.sql.Datasource from [Module \"deployment.MyApp.war:main\" from Service Module Loader]"}}
17:18:30,014 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015877: Stopped deployment MyApp.war in 7ms
17:18:30,019 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"MyApp.war\".POST_MODULE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"MyApp.war\".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of deployment \"MyApp.war\"
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011033: Could not load javax.sql.Datasource referenced in env-entry
Caused by: java.lang.ClassNotFoundException: javax.sql.Datasource from [Module \"deployment.MyApp.war:main\" from Service Module Loader]"}}}}