Java GWT-JNDI-DB连接错误

Java GWT-JNDI-DB连接错误,java,gwt,jndi,Java,Gwt,Jndi,我正在将我的工作应用程序转换为使用JNDI来解决部署到服务器时的超时问题。当我在本地进行测试时,会出现以下错误(我已确保在运行此测试时使用context.xml中的本地路径): 代码是: public class MySQLConnection extends RemoteServiceServlet implements DBConnection { //TODO // •Manage the connection in single class for whole application.

我正在将我的工作应用程序转换为使用JNDI来解决部署到服务器时的超时问题。当我在本地进行测试时,会出现以下错误(我已确保在运行此测试时使用context.xml中的本地路径):

代码是:

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>

最大尺寸
3145728
懒虫
0
org.AwardTracker.server.ServletContextImpl
mySQLConnection
org.AwardTracker.server.MySQLConnection
上传servlet
org.AwardTracker.server.MyCustomisedUploadServlet
mySQLConnection
/org.AwardTracker.AwardTracker/MySQLConnection
上传servlet
*古普尔德先生
AwardTracker.html
非常感谢你的帮助

问候,


Glyn

context.xml是一个特定于Tomcat的文件,您使用的是Jetty(我想您使用的是GWT DevMode),它的配置不同


这就是说,DevMode中的JNDI可能可以工作,但没有得到官方支持。您更应该使用根据您的需要定制的外部服务器(可能更接近您的生产环境)

@user3218114您好,如上所示,我终于开始实施JNDI了。你能看看这个问题吗?你好,格林。你好,托马斯(@),是的,我正在使用GWT DevMode。为了测试每个更改,必须将其上传到主机的服务器上,这是非常耗时和不方便的。是否有其他方法可以让我在本地(即在笔记本电脑上)进行测试?我必须解释,我不是技术性的,我正在为我的童子军包开发这个。感谢您的帮助,Glyn。安装本地Tomcat实例几乎和下载、解包和启动一样简单。你甚至可以让IDE自动在那里部署你的应用程序。⇒ 嗨,托马斯(@stackoverflow.com/users/116472/Thomas broyer),我不知道你的意思。我已经有了一个本地Tomcat实例。在我改用JNDI之前,这一切都在工作,允许我进行本地测试。我已将war上传到主机的服务器上,它似乎正在连接到主机的数据库。我只是有一个错误的SQL的问题,我已经纠正,并等待新的战争来实施。因此,代码是正确的,因此我推测有一些设置我没有正确完成。在这方面,GlynI添加了“System.out.println(“DataSource ds=“+ds”);”返回“DataSource ds=null”。因此要处理的问题是:ctx=newinitialcontext();ds=(数据源)ctx.lookup(“java:comp/env/jdbc/mydatabase”);亲爱的,Glyne,这是部署到本地Tomcat时发生的吗?还是仍然在DevMode中?在DevMode中,这是预期的。如果它在本地Tomcat中,那么您可能想问另一个问题,或者更新您的问题()并重新标记它,因为这与GWT无关。
<?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>