Java / GWT / MySQL: connection failed?

I am developing a GWT web application and trying to connect to a mysql database from my RemoteServiceServlet implementation (for rpc).

The server is running, and I can connect to the database from the machine I'm working on. For testing purposes, I wrote a small program that connects only to the database. It works:

Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306/dbname", "dbuser", "dbpasswd"); 

But the same lines do not work in my gwt application with 100 stack trace lines, here is the "reason":

 Caused by: java.net.SocketException: java.security.AccessControlException: access denied (java.net.SocketPermission 192.168.0.106:3306 connect,resolve) at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:408) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:269) at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294) ... 55 more 

and now my question is: why?

edit: for testing I use development mode with chrome and gwt plugin

edit2: part of the code where it crashes:

 public DataLinkImpl() { Connection tmpConnection = null; try { Class.forName("com.mysql.jdbc.Driver"); tmpConnection = DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306/dbname", "dbuser", "dbpasswd"); } catch (ClassNotFoundException e) { System.err.println("jdbc driver class not found"); } catch (SQLException e) { System.err.println("could not establish database connection"); e.printStackTrace(); e.getNextException().printStackTrace(); } finally { connection = tmpConnection; } } 

with the following glass:

 could not establish database connection com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112) at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116) at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2333) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112) at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:185) at de.eikecochu.gwt.awidb.server.DataLinkImpl.<init>(DataLinkImpl.java:23) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153) at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428) at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:60) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:78) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:362) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) Caused by: java.net.SocketException: java.security.AccessControlException: access denied (java.net.SocketPermission 192.168.0.106:3306 connect,resolve) at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:408) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:269) at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294) ... 55 more 01.12.2011 02:33:09 com.google.appengine.tools.development.ApiProxyLocalImpl log SCHWERWIEGEND: javax.servlet.ServletContext log: unavailable java.lang.NullPointerException at de.eikecochu.gwt.awidb.server.DataLinkImpl.<init>(DataLinkImpl.java:29) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153) at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428) at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:60) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:78) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:362) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) 01.12.2011 02:33:09 com.google.apphosting.utils.jetty.JettyLogger warn WARNUNG: /awidb/datalink java.lang.NullPointerException at de.eikecochu.gwt.awidb.server.DataLinkImpl.<init>(DataLinkImpl.java:29) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153) at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428) at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:60) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:78) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:362) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) 

edit3: I added permission to my /Library/Java/.../security/java.policy file:

 grant { permission java.net.SocketPermission "192.168.0.106:3306", "connect,resolve"; } 

but still the same, I can’t connect to the database. I also tried to execute it directly on the machine where the db is located, so I deployed it to tomcat (the same permission was added there), but still not needed. Wtf ??

edit4: to confirm this, I wrote a very simple web application:

EntryPoint Class:

 package de.eikecochu.gwt.gwttest.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; public class GWTTest implements EntryPoint { private TestServiceAsync testservice = GWT.create(TestService.class); @Override public void onModuleLoad() { Button b = new Button("test"); RootPanel.get("container").add(b); b.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { testservice.echo("test", new AsyncCallback<String>() { @Override public void onFailure(Throwable caught) { RootPanel.get("container").add(new Label("error")); } @Override public void onSuccess(String result) { RootPanel.get("container").add(new Label(result)); } }); } }); } } 

Remote Service Interface:

 package de.eikecochu.gwt.gwttest.client; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; @RemoteServiceRelativePath("testservice") public interface TestService extends RemoteService { String echo(String s); } 

Remote Service Asynchronous Interface:

 package de.eikecochu.gwt.gwttest.client; import com.google.gwt.user.client.rpc.AsyncCallback; public interface TestServiceAsync { void echo(String s, AsyncCallback<String> callback); } 

RemoteServiceServlet implementation:

 package de.eikecochu.gwt.gwttest.server; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import de.eikecochu.gwt.gwttest.client.TestService; public class TestServiceImpl extends RemoteServiceServlet implements TestService { private static final long serialVersionUID = 1L; private final Connection connect() { String driver = "com.mysql.jdbc.Driver"; String dblink = "jdbc:mysql://192.168.0.106:3306/"; String dbname = "dbname"; String dbuser = "dbuser"; String dbpass = "dbpass"; try { Class.forName(driver).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } Connection conn = null; try { conn = DriverManager.getConnection(dblink + dbname, dbuser, dbpass); } catch (SQLException e) { System.err.println("mysql connection error: "); e.printStackTrace(); } return conn; } @Override public String echo(String s) { Connection c = connect(); return s; } } 

this doesn't work either. Not on my machine, not on localhost (where db is located). The same error appears again (AccessControlException bla bla).

edit5: I tried to run my application directly on my server, which means: I installed the eclipse and gwt tools on the server and ran them there locally in chrome / development mode. Still the same error. I get the feeling that either my mysql / ubuntu has the wrong configs, or I'm doing something wrong, in principle. Usually, just connecting to the database is enough, right? I mean, what is a website without a database ...

edit6: as the woman suggested, I tried to set System.setSecurityManager (zero);

in my RemoteServlet right before connecting to the database. It produces the following error (abbreviated):

 java.security.AccessControlException: access denied (java.lang.RuntimePermission setSecurityManager) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374) at java.security.AccessController.checkPermission(AccessController.java:546) at java.lang.SecurityManager.checkPermission(SecurityManager.java:532) at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:252) at java.lang.System.setSecurityManager0(System.java:273) at java.lang.System.setSecurityManager(System.java:264) 

so it looks like I have SecurityManager installed.

In addition, I tried to access my database using telnet. At first, my connection was refused, but I changed the mysql-bind address to the ip of my server (192.168.0.106) and performed the following steps: http://ubuntu-commands.blogspot.com/2008/11/how-do-i- enable-remote-access-to-mysql.html

And when I telnet 192.168.0.106 3306 strange characters now appear:

 eikes-macbook-air:~ eike$ telnet 192.168.0.106 3306 Trying 192.168.0.106... Connected to eike-server.speedport.ip. Escape character is '^]'. = 5.1.58-1ubuntu1(b5|529h~!dYpI\rHP:qKc ot packets out of orderConnection closed by foreign host. 

Seems to be a mistake?

Also, I searched for the AccessDenied error and found this link: http://code.google.com/p/gwt-examples/wiki/project_MySQLConn#Tomcat_Setup_Notes which accurately describes the error I am experiencing, although I use Devmode for development. Although the following steps did not help (I edited the default / Library /.../ security / java.policy file)

edit7: I installed postgresql and installed a very simple database to check the connection. Without GWT, it works fine, GWT again produces the same error. Therefore, this is not a mistake in my mysql configuration. I played a little with the Java policy file, without reaction. GWT hates me:

edit8: I tried both postgresql and mysql on another machine (win7 this time) and ran my gwt application there locally (before and after editing the policy file), still the same error. This is an unmistakable GWT error! It seems that gwt devmode is not allowed to do anything anywhere ...

Another question: the error explicitly reads: "java.security.AccessControlException: access denied (java.net.SocketPermission localhost resolve)", so why does it still appear when I change my Java policy file to connect, allow the ports to be used? It seems to have absolutely no effect! I even tried providing AllPermission, no effect.

Next, I found "gwt-log.txt", which lists errors during the boot process:

 Public resources found in... [WARN] Error processing classpath URL 'file:/C:/Program Files (x86)/eclipse/plugin/com.google.gdt.eclipse.designer.hosted.2_2_2.4.2.r37x201110201837/gwt-dev-designtime.jar' java.net.URISyntaxException: Illegal character in path at index 16: file:/C:/Program Files (x86)/eclipse/plugins/com.google.gdt.eclipse.designer.hosted.2_2_2.4.2.r37x201110201837/gwt-dev-designtime.jar at java.net.URI$Parser.fail(Unknown Source) 

I do not know what it is or if it is important, but it is not my fault!

+6
source share
3 answers

The Google Eclipse plugin with the Google App Engine is installed. Create a new GWT project and make sure you deselect Google App Engine, and then try again. Or remove all jar files related to GAE from your eclipse class path.

GAE is a hosting environment provided by Google. It has a very limited environment. It does not allow opening socket connections or reading / writing files. Database drivers open a socket connection and therefore GAE blocks it with a security exception.

GWT and GAE are two separate Google offerings, but the Eclipse plugin is the same. Unfortunately, it is assumed that you want to use both offers as part of your project.

+6
source

It seems to me that your SecurityManager policy does not allow you to do certain things that you do. Are you sure your java.policy file is being applied? Can you try calling `System.setSecurityManager (null); 'and try to connect to your db?

If during a call to setSecurityManager you get a SecurityException, it seems to me that you have a SecurityManager. In addition, as the released guy said:

Keep in mind that by default mysql does not listen on non-localhost interfaces, you will need to open them yourself.

Can you try to connect to the above address and the specified port and see if everything is all right?

EDIT:

The more I look at things, the more I think this is a MySQL configuration problem. Do you execute your pure Java code from the same computer on which you tried to connect to GWT? How could your clean java code connect even if your telnet failed? Can you check this behavior again?

+4
source

Keep in mind that by default mysql does not listen on non-localhost interfaces, you will need to open them yourself.

+1
source

Source: https://habr.com/ru/post/902806/


All Articles