我正在转换我的工作应用程序,以便在部署到服务器时使用JNDI来解决超时问题。当我在本地测试时,我收到以下错误(我确保在运行此测试时我在context.xml中使用本地路径):
Context Created
Error connecting to database - not good eh
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
Starting Jetty on port 8888
[WARN] Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract org.AwardTracker.client.Account org.AwardTracker.client.DBConnection.authenticateAccount(java.lang.String,java.lang.String)' threw an unexpected exception: java.lang.NullPointerException
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)
代码是:
public class MySQLConnection extends RemoteServiceServlet implements DBConnection {
//TODO
// •Manage the connection in single class for whole application.
// •Initialise the data source at application start up single time.
// •Don't handle any exception in service implementation just throw it to client or if handled then re-throw some meaning full exception back to client.
// •Add throws in all the methods for all the RemoteService interfaces whenever needed.
private static final long serialVersionUID = 1L;
// Get DataSource from JNDI (defined in context.xml file)
Context ctx = null;
DataSource ds = null;
public MySQLConnection() {
try {
// Get DataSource from JNDI (defined in context.xml file)
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mydatabase");
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
//NEVER catch exceptions like this
System.out.println("Error connecting to database - not good eh");
e.printStackTrace();
}
}
我的context.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mydatabase"
auth="Container"
type="javax.sql.DataSource"
username="glyndwr"
password="***********"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/glyndwr?autoReconnect=true"
validationQuery="select 1"
removeAbandoned="true"
removeAbandonedTimeout="120"
logAbandoned="true"
maxWait="60"
maxActive="10"
maxIdle="4"/>
<!-- url="jdbc:mysql://localhost/glyndwr?autoReconnect=true"; -->
<!-- url="jdbc:mysql://mysql5.metawerx.net:3506/glyndwr?autoReconnect=true" -->
</Context>
我的web.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<!-- max size of the upload request -->
<param-name>maxSize</param-name>
<param-value>3145728</param-value>
</context-param>
<context-param>
<!-- Useful in development mode to slow down the uploads in fast networks.
Put the number of milliseconds to sleep in each block received in the server.
false or 0, means don't use slow uploads -->
<param-name>slowUploads</param-name>
<param-value>0</param-value>
</context-param>
<!-- ServeletContext Implementation for memory leaks -->
<listener>
<listener-class>
org.AwardTracker.server.ServletContextImpl
</listener-class>
</listener>
<servlet>
<servlet-name>mySQLConnection</servlet-name>
<servlet-class>org.AwardTracker.server.MySQLConnection</servlet-class>
</servlet>
<!-- servlet>
<servlet-name>uploadServlet</servlet-name>
<!- This is the default servlet, it puts files in session -
<servlet-class>gwtupload.server.UploadServlet</servlet-class>
</servlet -->
<servlet>
<servlet-name>uploadServlet</servlet-name>
<servlet-class>org.AwardTracker.server.MyCustomisedUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mySQLConnection</servlet-name>
<url-pattern>/org.AwardTracker.AwardTracker/MySQLConnection</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>uploadServlet</servlet-name>
<url-pattern>*.gupld</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>AwardTracker.html</welcome-file>
</welcome-file-list>
</web-app>
非常感谢您的帮助。
此致
格林
答案 0 :(得分:0)
context.xml
是一个特定于Tomcat的文件,你正在使用Jetty(我想你正在使用GWT DevMode),它的配置不同。
那就是说,DevMode中的JNDI可能会工作但是没有得到官方的支持。您应该使用根据您的需求量身定制的外部服务器(可能更接近您的生产环境)