I know my message can seem a duplicate of another, but I don't think so. Tell me if I'm wrong.
I just finished a free software in Java/swing/postgresql. It was successfully tested on Windows and Linux.
Now I am trying to make the documentation of it, and I'm on the "Installing postgresql server" part of the documentation.
The problem is that I can't remember what I've done to make the application work with PostgreSQL (I know it's lame...).
The JDBC string is made this way in the code:
// Open the SQL Connection
m_sql_connection = DriverManager.getConnection("jdbc:postgresql://"+m_postgresql_server_name+":"+m_postgresql_port_number+"/"+m_postgresql_database_name, "postgres", "<not empty>");
The facts:
I work on a Windows 7 machine 64 bit
The PostgreSQL server is running (I see the service Started)
The PostgreSQL server configuration seems OK
I use login: postgres / pass: in my JDBC string
I can make the queries I want with pgAdmin III and they work
The database server is on the same machine as the "*.jar" application file
The jar application works well on a Linux version and another Windows 7 32 bit machine (the development machine)
I disabled my antivirus (just in case...)
psql.exe works well (I could insert data with it)
Edit 1 -> JJF
- I just added postgresql.exe and pg_ctl.exe in the windows firewall, and checked Both checkboxes for each (Public and Home/Work(Private) )
Result: Same Error
Edit2 -> Hardik Patel
- Skype is already OFF
Edit 3 -> JJF
- After full stop of Windows Firewall, still the same problem
The error I get is this one:
Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
I wonder if I don't have to "unlock" the postgres account to allow it to access data through JDBC.
Edit 4 - Horse with no name
- I replaced localhost by 127.0.0.1
- The port is the standard port 5432, validated with psql
- I tested the psql.exe (working) command line before trying the JDBC connection (not working)
- Still the same result...
Edit 5
- I am going to put some trace to see if i can get more information
Turn off skype if running because skype acquires same port. This trick helped me long time ago.
Check that Windows firewall is not blocking the port.
I corrected my problem by a workaround.
I changed the PostgreSQL listen port by 5433 and everything worked like a charm...
I think the original port is used by something else, or blocked by some security tool I could not find.
Related
I'm trying to learn some basic JDBC.
I've created a JavaDB server and made a Java program where i can read
and alter some tables in the database.
I thought it would be a fun next step to be able to connect to the
server from another computer running on the same network, or even
from the internet if it's similar.
So far I’ve just tried opening project on another computer to see if
it would connect to the localhost, but would expected it to be too
easy...
The current db name is "jdbc:derby://localhost:1527/test;user=db;password=db".
Could i replace the localhost with my ipv4 address to connect
locally? or
Do i really need the router's IP and do some port forwarding?
Connecting locally is really what I need. If this is easier, then that's what i would like to do.
You need to use either the hostname or the IP of the box you want to connect to:
URI: jdbc:derby://<hostname_or_ip>:1527/test;user=db;password=db
Replace <hostname_or_ip> for the real value and you are good to go.
You need to open the port 1527 on your router.
And you can try this answer
https://stackoverflow.com/a/12176840/2746009
So, i will recommend you to see the firewall settings, if firewall is not blocking the way or you can give a try to communicate to another port. If both of these things didn't work, let me know, how far it takes before spitting this error out. Also, in command prompt, try ping machineIP and see if it's sending and receiving data
I was doing the same configuration and after spending some hours I found the solution:
http://db.apache.org/derby/docs/10.2/adminguide/derbyadmin.pdf
Page 34:
By default, the Derby Network Server will only listen on the localhost (...)
One solution is to create a [derby.properties] file with the following content:
derby.drda.host=0.0.0.0
The place where you save the properties file may vary:
- in one machine for me it was directly under 'javadb', sibling of 'bin', 'lib', etc
- at my 2nd machine it was under the 'bin' directory, sibling of *.bat files.
To verify if your properties is being read (it doesn't exist at first, Derby doc explicitly tells that it won't create it for you), is to add this property as well:
derby.stream.error.file=derbyNewLog.log
If you execute
bin/startNetworkServer
it will create the new log file in case your properties is correctly being read.
After you confirm that your new properties file is being read, you can use the bin/ij tool to execute connect statements to your DB (both local and remote). This is how I could verify it quickly (my db name is 'differentRequestsAndPerformance'):
c:\glassfish4\javadb\bin>ij
ij version 10.10
ij> connect 'jdbc:derby://localhost:1527/differentRequestsAndPerformance';
ij> show tables;
TABLE_SCHEM |TABLE_NAME |REMARKS
------------------------------------------------------------------------
SYS |SYSALIASES |
(...)
ij> disconnect;
>> this 'connect' with localhost is expected to work always - you can append ';create=true' to the DB name in case it doesn't exist already; now change it to your remote ip (I'm assuming you previously confirmed with 'ping' command that you actually can see the server remote machine, otherwise it's a network problem, not a Derby problem)
ij> connect 'jdbc:derby://192.168.1.14:1527/differentRequestsAndPerformance';
ij> show tables;
TABLE_SCHEM |TABLE_NAME |REMARKS
------------------------------------------------------------------------
SYS |SYSALIASES |
(...)
ij> disconnect;
One additional comment: I'm not sure how much the 0.0.0.0 setting will open your machine to malicious software because I only wanted to verify my code and I disabled it right after I was satisfied. More research on this is required in case you want to use this for real programs.
In case your 'ij' tool complains about some driver, add the javadb/lib directory to the CLASSPATH system variable.
Another thing (obvious but better safe than sorry): this configuration must be set at the remote db (where your requests will arrive), in case like me you have 2 machines both with the database running.
I am facing below issue.
1- I have data source on my local websphere6.1 and when i do test connection from web console server ,it was successful but when i tried to do the same from my application code it giving me error
java.sql.SQLException: Io exception: The Network Adapter could not establish the connectionDSRA0010E: SQL State = null, Error Code = 17,002
Note : I am connecting to remote oracle db in my machine , I have only raid,toad & web sphere installed.
I have already tried following things but issue is yet not resolved.
Replace my server names with the ip addresses.
Change minimum connection setting from web sphere server configuration
The same configuration is running to my other colleague but issue is occurring only on my laptop.
Please any body help me.
Advance thank you
Some things you can try, seeing your error:-
-> Verify that oracledb is started, also verify the port db is listening on is correct.
-> If you have firewall in between and you can disable it, disable it and try. Or else try adding the WAS ports in authorized lists of firewall.
After lot of searching and did lots of trick , i resolved this issue and this solution is Formatting my machine.
Some time there might be issue with network adapter due to this it create problem . I have tried below thing before formatting my machine.
1- Disable my symantic anti-virus but not work.
2- Adding oracle db ip in antivirus configuration and allow all traffic.
3-Tried to change from server admin console by changing (min connection ,max connection , componenet managed authentication
4- Finally ,un-install RAID/websphere . Still again problem persist.
I have spent lot of time and do Much R&D but finally solution come by formatting my laptop.
I resolved the issue after that I've changed the database port that I used to connect at the bd. Check on whitch port your db has been started.
You can check the official IBM developer network issue at:
https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014687717
Regards.
Please change the JVM argument through the WAS Admin console while connecting the JNDI connection. Below is value :
-Djava.net.preferIPv4Stack=true
Path to set JVM:
Go to the server and select Websphere application server and select server name like server1
Select Java and Process Management at the right side
Select Process definition
Select Java Virtual Machine
Enter above value (-Djava.net.preferIPv4Stack=true) in Generic JVM arguments and Save it and restart the server.
It resolved my problem!
I'm attempting to get a development environment up and running on my OSX laptop, creating JSPs within Eclipse, running with a Tomcat server, and connecting to an Oracle database.
Eclipse and Tomcat are playing nice together, and I can make JSPs and so forth. The hard part now is getting an Oracle database up and running on Mountain Lion, and getting Eclipse to connect to it.
After extensive Googling, I have found a number of resources:
http://dimitrisli.wordpress.com/2012/08/08/how-to-install-oracle-database-on-mac-os-any-version/
http://barrymcgillin.blogspot.com/2011/12/using-oracle-developer-days-virtualbox.html
(or even https://docs.google.com/document/d/1Th5MSIhS13YIJYCD8W1GLnOQEfrfov-92-He1cluTec/pub)
Following these (rather thorough) guides, I have downloaded the latest Virtual Machine from Oracle, here:
http://www.oracle.com/technetwork/database/enterprise-edition/databaseappdev-vm-161299.html
Note: All three of the above tutorials use a slightly older version of the VM, which has Oracle DB 10 or 11, but the one available now is 12c. The new one also has a different version of Oracle Linux, and probably some other differences. Various usernames and passwords seem to be slightly different.
I installed VirtualBox, loaded in the VM, brought it online. I have the Network settings configured with just a NAT adapter, with the adapter type as "PCnet-FAST III", and port forwarding from 127.0.0.1 to 10.0.4.15 on port 1521 to 1521 and 2222 to 22.
Now, if I open Terminal on my host machine, I can SSH into my VM via: ssh -p 2222 oracle#localhost. I get the Terminal in the VM, and it gives me the same welcome message I get when I first boot up the VM in VirtualBox. Also, I can ping localhost or 127.0.0.1 and it responds with a variable but short lag.
Based on this I can only assume that my VM is working, and that my port forwarding (which is laid out in all 3 of the tutorials I linked to up there) is in good shape.
Now comes the part where I would want to connect Eclipse to the Oracle database that's within the VM. If you look under the first tutorial, dimitrisli sets up IntelliJ with a datasource pointing to the VM. I want to do the same within Eclipse.
I am new to Eclipse, so I looked around for exactly how to do this, and I came up with:
1) Make a new Database Driver.
I downloaded the JDBC for Oracle Database 12c, JDK 7, from Oracle, here. I stuck the .jar file in my Documents folder (in case that's wrong). Within Eclipse I made a new Database Driver, based off one of the other Oracle Driver templates, and changed the settings. Under "Driver Files" I pointed to my ojdbc7.jar file, and under "Properties" I have the following--let me know if this is wrong:
Catalog: USER
Connection URL: jdbc:oracle:thin:#localhost:1521:orcl
Database Name: pdb1
Driver Class: oracle.jdbc.OracleDriver
Password:
User ID:
I tried to use what came in the Oracle Driver template and just tweak to match my exact scenario (based on what was said in the walkthroughs and what I could divine from poking around the modern version of the VM).
2) Make a new Database Connection.
When I go to make a new Database Connection, I go to the Driver Properties area, and I can pick my custom Driver. There is also a Properties field, which I enter the following into: (some of it seems to overwrite or duplicate what I typed in when I made the Driver itself?)
SID: orcl
Host: 127.0.0.1
Port Number: 1521
User Name: hr // Gotten from one of the tutorials. Have also tried 'system/oracle', neither works yet.
Password: oracle
Connection URL: jdbc:oracle:thin:#127.0.0.1:1521:orcl // Immutable, set by the Driver I suppose.
Catalog: User
When I click Test Connection, it hangs for a while and then I get a 'Ping failed!' error, with details:
java.sql.SQLRecoverableException: IO Error: Socket read timed out
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:298)
at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:81)
at org.eclipse.datatools.enablement.internal.oracle.JDBCOracleConnectionFactory.createConnection(JDBCOracleConnectionFactory.java:27)
at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Caused by: oracle.net.ns.NetException: Socket read timed out
at oracle.net.ns.Packet.receive(Packet.java:350)
at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:153)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
... 14 more
Unfortunately, being new to Java and Eclipse and Oracle DB, I'm not quite sure what to do with this, and I have been poking around for several hours trying to determine what could be wrong. I'm hoping someone with some experience on this can point me in the right direction.
(Oh, and I'm not an inexperienced developer--just new to these tools.)
Thanks!
I solved my issue, after roughly 6 or 7 hours wasted.
Thanks to this post:
Virtualbox "port forward" from Guest to Host
And specifically to the comment from #Nicholas which made me realize that I had deactivated my Mac's firewall, but not the firewall in the guest OS. Turning that off made everything work suddenly.
I suppose that the default configuration of the guest OS's firewall did allow for SSH on port 2222, as that worked fine even with the firewall in place, which served to mislead me into thinking that there was something wrong with my database driver or connection.
Hopefully this will benefit posterity.
EDIT:
As #HarpreetDawar mentions, the correct connection string to access the database is:
jdbc:oracle:thin:#localhost:1521/PDB1
The one that I was using, ending in :orcl instead of /PDB1, connects to the "container database". I don't fully understand Oracle 12c and the "pluggable database" implementation, but the rough idea is that you can have multiple databases within a single one, and you can turn them on and off by plugging/unplugging them. Hence, "PDB1", Pluggable Database 1.
If you connect to the Container, you will find that you are unable to create a new User/Schema without prefixing it with "C##", which is a tad awkward. That's because the Container is not meant to be used as a normal DB. You connect to the Container to plug/unplug the pluggable dbs (and other things, no doubt).
So if you want to connect to the Container, use a colon and the "SID" (orcl in the VM) in the connection string, and if you want to connect to a Pluggable, use a forward slash and the "Service Name" (PDB1 in the VM) in the connection string.
Anyway, this is additional data that I learned after solving my original problem.
One more thing! A note on port forwarding.
Let's say you now have your VM up and running, and you can connect to your Oracle DB properly, etc. But you have a friend who wants to use the DB with you? Well, that's easy. He should just use the same connection string, with your IP address instead of localhost, and the port forwarding that you set up in VirtualBox should send his connection (using port 1521) straight into your VM. Right?
Well, if you set up your Port Forwarding as From: 127.0.0.1, it won't work, because your buddy's connection is going to your external IP address. So add an additional port forwarding setting, from your external IP address to your VM. Then it will work great!
-Matt
I am the author of the first guide Matt Mc is quoting and taking the liberty to post my comment to his answer as a separate answer for visibility purposes.
I've initially written this guide ~ 2 years ago explaining in detail the process to get the Oracle 11gR2 installed on a Mac using VirtualBox.
Yesterday (25 Apr 14), I've upgraded the same guide outlining all the extra steps needed to get Oracle 12cR1 installed on a Mac using VirtualBox.
Use the following url
url = jdbc:oracle:thin:#//127.0.01:1521/PDB1
to make it work.
So I wrote a JApplet that connects to and does some stuff with a MySQL server.
When I test it in Eclipse it works just fine. I have the IP address, the database name, the username I'm using (root) and the password hardcoded into the program at the moment.
If I have the IP as localhost, or 127.0.0.1 (same thing but yeah), or 192.168.x.x (my PC's local address) the JApplet while being run from Eclipse will connect to the MySQL server with no issues. Also, if I open another terminal and login to mysql that way it also works.
However, if I jar the project using Eclipse, include the mysql driver in the HTML applet tag and run it through a web browser on the same machine it does not work.
Here is the HTML tag that I am using:
<applet code="mysqlApp.MysqlInterface.class"
archive="LoginDemo.jar, mysql-connector-java-5.1.18-bin.jar"
height="150" width="450">
Your browser does not support the <code>applet</code> tag.
</applet>
I have tried it in Firefox 10.* with port 3306 forwarded on my router and my software firewall turned off. Also, the MySQL server is bound to 0.0.0.0 (it accepts connection attempts on all interfaces) and TCP/IP is enabled (skip-networking option is not used). I get a message dialog window saying that no sql connection can be made (what I programmed it to do) and I get the following exception in the Java console:
SQLException: 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.
SQLState: 08S01
VendorError: 0
I also tried it in the latest version of Internet Explorer for Windows 7 x64 and when I click on the buttons that are supposed to connect it to the server the webpage freezes.
Nothing prints out to the console where mysqld is running (I ran it with an admin terminal using mysqld --console)
Does anybody have any ideas? I have been looking for a solution for days but everything I find that seems like it may work has one or two little details that makes it not applicable to my issue.
Yes, I know that I should not be having the applet communicate directly with the database if this is to be used in production code (major security issue) but I am in the early stages of doing this and am trying to figure this out for testing purposes.
Thanks.
You should have your applet communicate to some backend (java or php) through ajax. Then use the backend to communicate with mysql. A good place for ajax is http://prototypejs.org/assets/2010/10/12/prototype.js.
This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 6 years ago.
My program that connects to a MySQL database was working fine. Then, without changing any code used to set up the connection, I get this exception:
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.
What happened?
The code used to get the connection:
private static Connection getDBConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
String username = "user";
String password = "pass";
String url = "jdbc:mysql://www.domain.com:3306/dbName?connectTimeout=3000";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
This is a wrapped exception and not really interesting. It is the root cause of the exception which actually tells us something about the root cause. Please look a bit further in the stacktrace. The chance is big that you'll then face a SQLException: Connection refused or SQLException: Connection timed out.
If this is true in your case as well, then all the possible causes are:
IP address or hostname in JDBC URL is wrong.
Hostname in JDBC URL is not recognized by local DNS server.
Port number is missing or wrong in JDBC URL.
DB server is down.
DB server doesn't accept TCP/IP connections.
Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
To solve the one or the either, follow the following advices:
Verify and test them with ping.
Refresh DNS or use IP address in JDBC URL instead.
Verify it based on my.cnf of MySQL DB.
Start it.
Verify if mysqld is started without the --skip-networking option.
Disable firewall and/or configure firewall/proxy to allow/forward the port.
By the way (and unrelated to the actual problem), you don't necessarily need to load the JDBC driver on every getConnection() call. Just only once during startup is enough.
check your wait timeout set on the DB server.
Some times it defaults to 10 seconds. This looses the connection in 10 seconds.
mysql> show global variables like '%time%' ;
update it make it something like 28800
mysql> SET GLOBAL wait_timeout = 28800;
I've been having this issue also for about 8-9 days.
Here's some background: I'm developing a simple Java application that runs in bash.
Details:
Spring 2.5.6
Hibernate3.2.3.ga
With maven.
(The base of the project is from mkyong.com , the spring tutorial without anotations )
MySQL version:
[jvazquez#archbox ~]$ mysql --version
mysql Ver 14.14 Distrib 5.5.9, for Linux (i686) using readline 5.1
Linux archbox 2.6.37-ARCH #1 SMP PREEMPT Fri Feb 18 16:58:42 UTC 2011 i686 Intel(R) Core(TM)2 Quad CPU Q8200 # 2.33GHz GenuineIntel GNU/Linux
The application works fine in Arch Linux, Mac OS X 10.6, and FreeBSD 7.2.
When I moved the jar file to another arch linux in a different host, using the same mysql, a similar my.cnf, and the similar kernel version, the connection died and obtained the same error as the original poster:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
I tried every possible combination for this that I found on so and the forums (http://forums.mysql.com/read.php?39,180347,180347#msg-180347 for example, which is closed now and I can't post .. ), specifically:
Triple check that I wasn't using skip
networking. (verified with ps aux
and the my.cnf)
Tried enable log_warnings=1 in the my.cnf but obviously, I wasn't hitting the
server so I didn't saw anything while using the app
SHOW ENGINE innodb STATUS didn't show anything at all; during the tests I could connect via shell, and php also connected to the mysql server
/etc/hosts has localhost 127.0.0.1
Tried the jdbc properties using localhost and 127.0.0.1 with no results
Tried adding c3p0 and changed the max_wait
Max connections in the my.cnf was changed to 900 , 2000 and still nothing my.cnf
Added wait_timeout = 60 my.cnf
Added net_wait_timeout = 360 my.cnf
Added the destroy-method="close" spring.xml
As it was pointed out (if you look up for the same exception , you will find several so threads about the issue Reproduce com.mysql.jdbc.exceptions.jdbc4.CommunicationsException with a setup of Spring, hibernate and C3P0
for example ).
If you are using tomcat, please check the security exception (again, it is on SO, you will find it )
Check that you can resolve that url that you are using
Try adding c3p0.
Verify that there isn't a firewall rejecting your connections
Finally , if you are using GNU/Linux ( ARch linux for example and you indeed obtain this exception )
Try
MySQL Forums :: JDBC and Java :: EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost
If the link get's removed, just add mysqld:ALL to /etc/hosts.allow
I know that is a bit extense, but it may help anybody using GNU/Linux and having this exception and this thread seemed the best place to post my research.
Hope it helps
I got the same error
but then I figured it out its because the Mysql server is not running at that time.
So to change the status of the server
Go to Task Manager
Go to Services
then search for your Mysql server(eg:for my case its MYSQL56)
then you will see under the status column it says its not running
by right clicking and select start
Hope this will help.
We have a piece of software (webapp with Tomcat) using Apache commons connection pooling, and worked great for years. In the last month I had to update the libraries due to an old bug we were encountering. The bug had been fixed in a recent version.
Shortly after deploying this, we started getting exactly these messages. Out of the thousands of connections we'd get a day, a handful (under 10, usually) would get this error message. There was no real pattern, except they would sometimes cluster in little groups of 2 to 5.
I changed the options to on the pool to validate the connection every time one is taken from or put back in the pool (if one is found bad, a new one is generated instead) and the problem went away.
Have you updated your MySQL jar lately? It seems like there may be a new setting that didn't used to be there in our (admittedly very old) jar.
I agree with BalusC to try some other options on your config, such as those you're passing to MySQL (in addition to the connection timeout).
If this failure is transient like mine was, instead of permanent, then you could use a simple try/catch and a loop to keep trying until things succeed or use a connection pool to handle that detail for you.
Other random idea: I don't know what happens why you try to use a closed connection (which exception you get). Could you be accidentally closing the connection somewhere?
Ensure skip-networking is commented out in my.cnf/my.ini
As BalusC mentioned, it would be very useful to post the full stacktrace (always post a full stacktrace, it is useless and frustrating to have only the first lines of a stacktrace).
Anyway, you mentioned that your code was working fine and that this problem started suddenly to occur without any code change so I'm wondering if this could be related to you other question Problem with not closing db connection while debugging? Actually, if this problem started while debugging, then I think it is (you ran out of connections). In that case, restart you database server (and follow the suggestions of the other question to avoid this situation).
I encountered same problem. I am using spring & dbcp & mysql 5.5But If I change localhost to 192.168.1.110 then everything works. What make things more weird is mysql -h localhost just works fine.
update: Finally found a solution. Changing bindaddress to localhost or 127.0.0.1 in my.conf will fix the problem.
In my case, the local loopback interface wasn't started, so "localhost" couldn't be resolved.
You can check this by running "ifconfig" and you should see an interface called "lo". If it is not up, you can activate it by running "ifup lo" or "ifconfig lo up".
In my case, the mysql.com downloaded Connector/J 5.1.29 .jar had this error whereas the 5.1.29 .jar downloaded from the MvnRepository did not.
This happened when building a Google appengine application in Android Studio (gradle, Windows x64), communicating to a Linux MySQL server on the local network/local VM.
I see you are connecting to a remote host. Now the question is what type of a network are you using to connect to the internet?
WINDOWS
If it's a mobile broadband device then get your machines IP address and add it to your hosting server so that your host server can allow connections coming from your machine.[your host might have turned this off due to security reasons].
Note that every time you use a different network device your IP changes.
If you are using a LAN then set a static IP address on your machine then add it to your host.
I hope this helps!! :)
I got the communications failure error when using a java.sql.PreparedStatement with a specific statement.
This was running against MySQL 5.6, Tomcat 7.0.29 and JDK 1.7.0_67 on a Windows 7 x64 machine.
The cause turned out to be setting an integer to a string parameter and a string to an integer parameter then trying to perform executeQuery on the prepared statement. After I corrected the order of parameter setting the statement performed correctly.
This had nothing to do with network issues as the wording of the error message suggested.
The escential problem is that Mysql JDBC pool connections is not used, then the Timeout from Mysql, close the Connections. You need change the pool Parameters to get restart connection when the connection has failures, on this way:
Connection Validation: Required (Check)
Validation Method: autocommit
You can change the Validation Method if you cannot get it works!
If you use WAMP, make sure it is online. What I did was, first turned my firewall off, then it worked, so after that I allowed connection for all local ports, specially port 80. Than I got rid of this problem. For me it was the Firewall who was blocking the connection.
I had the same problem and I used most of the params (autoreconnect etc..), but didn't try the (test_on_idle, or test_on_connect) , I am going to do them next.
However, I had this hack that got me through this:
I have a cron job called Healthcheck, It wakes up every 10 mins and makes a REST API call to the server. The web / app server picks this up, connects to the db, makes a small change and comes back with a 'yes all quiet on western front' or 'shitshappening'. When the latter, it sends a pager / email to the right people.
It has the side effect of always keeping the db connection pool fresh. So long as this cron is running, I don't have the db connection timeout issues. otherwise, they crop up.