UnknownHostException: while connecting to database server - java

I am trying to host MySQL database on 1nd1.com. I have created a database with few tables. Getting below error while connecting to 1and1 database from java application.
When pinging from the command prompt using "ping hostname" command, getting a failure in response.
Is there any way to connect to this host from java program?
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.UnknownHostException
MESSAGE: db726853948.db.1and1.com
STACKTRACE:
java.net.UnknownHostException: db726853948.db.1and1.com
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:907)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1302)
at java.net.InetAddress.getAllByName0(InetAddress.java:1255)
at java.net.InetAddress.getAllByName(InetAddress.java:1171)
at java.net.InetAddress.getAllByName(InetAddress.java:1105)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:246)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Contct.insertDetails(Contct.java:11)
at Contct.main(Contct.java:28)
** END NESTED EXCEPTION **
Last packet sent to the server was 0 ms ago.

try adding your hostname to your host file
for example:
your_host_ip(122.2.2.1) hostname(db726853948.db.1and1.com)

Related

Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException

when i run my code, 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:357)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2482)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:517)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Aug 25, 2015 8:23:45 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SCHWERWIEGEND: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:457)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy76.create(Unknown Source)
I don't know where is the problem. But yesterday i worked fine.
my database.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/streaming
jdbc.username=root
jdbc.password=password
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.showsql=true
hibernate.hbm2ddl.auto=create
It is possible for me to run mysql directly from command line. Where is the problem? Was is wrong.
I use spring 4 and hibernate with mysql as database language.
Has someone any ideas?
Thank.
There is CommunicationsException: Communications link failure.
If you get a SQLException: Connection refused or Connection timed out or a MySQL specific
CommunicationsException: Communications link failure
then it means that the DB isn't reachable at all. This can have one or more of the following causes:
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.
DB server has run out of connections.
Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
To solve the one or the other, 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 the DB.
Verify if mysqld is started without the --skip-networking option.
Restart the DB and fix your code accordingly that it closes connections in finally.
Disable firewall and/or configure firewall/proxy to allow/forward the port.
Source: More Details
I see that #Amit Tamrakar have good answer but I want to add:
if you make a lot of requests to db, (I mean connections), may be that application (some app thread) just wait for use free connection from app db connection pool, but throws timeout exception.
.h.engine.jdbc.spi.SqlExceptionHelper : HikariPool-1 - Connection is not available, request timed out after 30005ms.
Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
You can try to increase db connection pool in application config file, for example:
spring.datasource.hikari.maximum-pool-size=20
You can set size which you need. This config in app by default is 8.
And we need remember that each db have own free db connection size which we don't need exceed.
I mean if db have 5 free connection and you want to use 10 connections in your app then we will receive so exceptions.
DB is down.
You must start the DB server and check connectivity with either telnet or mysql command line client first.
I was not unable to figure out the exact cause, but I realized that it was because of some thread pools since I was able to connect to the db using the terminal.
for checking the connectivity you can use the below command.
psql -h hostname -U username -d database
so restarting the server worked for me.

Communications link failure issue

I hosted my first Java EE web site but my mySQL connections are timing out after few minutes and I am getting this error message:
HTTP Status 500 - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
type Exception report
message com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
description The server encountered an internal error that prevented it from fulfilling this request.
exception
dao.DAOException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
dao.OffreDaoImpl.lister(OffreDaoImpl.java:95)
filters.PrechargementFilter.doFilter(PrechargementFilter.java:96)
org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
root cause
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
sun.reflect.GeneratedConstructorAccessor28.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:525)
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2985)
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
com.jolbox.bonecp.PreparedStatementHandle.executeQuery(PreparedStatementHandle.java:172)
dao.OffreDaoImpl.lister(OffreDaoImpl.java:90)
filters.PrechargementFilter.doFilter(PrechargementFilter.java:96)
org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
root cause
java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2882)
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
com.jolbox.bonecp.PreparedStatementHandle.executeQuery(PreparedStatementHandle.java:172)
dao.OffreDaoImpl.lister(OffreDaoImpl.java:90)
filters.PrechargementFilter.doFilter(PrechargementFilter.java:96)
org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
I asked my hoster to increase the timeout value and here is his response:
Please accept our sincerest apologizes for misunderstanding.
Unfortunately due to shared hosting environment your request could not be >accomplished - it will affect other customers, that share server resources >for you. (10 min is max value for this parameter).
Since you strongly need some features - we can only suggest you considering >to move to VPS or dedicated server where you will have full admin >privileges and can do whatever you want without affecting of another >customers.
As a solution, we would like to suggest you to add in your jdbc connection >string following command:
&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
In this case, after timeout, you will be reconnected again.
So I edited my config.properties file:
url = jdbc:mysql://localhost:3306/myDatabase&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
driver = com.mysql.jdbc.Driver
username = xxxxxx
password = xxxxxx
But I am still facing the same issue.
Yes I am using localhost even when I host the website. But I tried to replace localhost string by the server address IP in the URL connection and I got this error message:
Caused by: java.sql.SQLException: Unable to open a test connection to the given database. JDBC url = jdbc:mysql://xxx.xxx.xxx.xx:3306/database_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10, username = user_name. Terminating connection pool. Original Exception: ------
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 10 times. Giving up.
So I noted the issue to my hoster and here is his reply:
Dear Customer,
Kindly use localhost instead IP address, as for your string, we actually asking you to not maintain connections as long as you wish. The thing is we have hard limit on opened connections overall = 300 and 50 per user.
And as this is shared server these connections are being allocated on demand to each user, and if your account consumes 50 connections it is 1/6 of all connections available.

Cannot create PoolableConnectionFactory error while starting application in Bluemix

Once I deploy my Java Application to IBM Bluemix, it can't create the initial database connection due to this error:
Building new Hibernate SessionFactory 2015-04-11 14:24:04 [main] ERROR
o.h.util.JDBCExceptionReporter - Cannot create
PoolableConnectionFactory (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.)
I tried to set the "hibernate.current_session_context_class" to "thread" (which solved the problem when I deployed the same app to Google Application Engine) but that didn't help with Websphere Liberty Profile:
<prop key="hibernate.current_session_context_class" >
thread
</prop>
Any ideas?
UPDATE 1 --- This is the actual root cause. Bluemix offers the ClearDB MySQL service but its pipeline cannot connect to it :(
Building new Hibernate SessionFactory 2015-04-11 15:04:49 [main] ERROR
o.h.util.JDBCExceptionReporter - 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.
UPDATE 2 --- Even the Experimental MySQL DB is also off limits if there's any attempt to connect from the pipeline machines:
o.h.util.JDBCExceptionReporter - Cannot create
PoolableConnectionFactory (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.)
org.hibernate.exception.GenericJDBCException: Cannot open connection
at
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
at org.hibe at
org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create
PoolableConnectionFactory (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
org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
at
org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at
org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 40 more Caused by:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
Please let me know if you have any ideas.
Ah ok. So this is happening because the Liberty buildpack autowires in JDBC connections for you during deployment to Bluemix. For the unit tests you will need to provide this connection info to your app.

Exception while connecting to mysql in 000webhost [duplicate]

This question already has answers here:
Java mySQL with 000webhost
(4 answers)
Closed 9 years ago.
I have created a database on 000webhost.com and now I am trying to connect to the database through java as:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://mysql14.000webhost.com/a2509514_Email", username, pass);
But when I execute it I am getting the following exception:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection timed out: connect
STACKTRACE:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at java.net.Socket.<init>(Socket.java:372)
at java.net.Socket.<init>(Socket.java:215)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at trapack.Experiment.sample(Experiment.java:20)
at trapack.Experiment.main(Experiment.java:35)
** END NESTED EXCEPTION **
Last packet sent to the server was 0 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at trapack.Experiment.sample(Experiment.java:20)
at trapack.Experiment.main(Experiment.java:35)
There is no direct discussion about this specific topic so any help would be really appreciated.
I got a similar problem when I was using MySQL in AWS (Amazon Web Services - Cloud Computing)
I had to give permission to my computer IP in the site (AWS).
I recommend you to use http://www.heidisql.com/ to test if you can connect to your database in 000webhost. If you succed, the error is in your Java code, if you fail to connect it can be your credentials, security configuration of your host site or just a horrible connection link you have there.
Give us more information about your problem so we can help you.
Use the connection string provided by 000webhost when you create the user.
000webhost.com creates their own string or host name for creating connection..
Hope it will works.
Thanks
The answer is you must upgrade your account.
The free MySQL from 000webhost doesn't allow you to connect from external applications, just from within pages hosted in their domain.
Please check: How can I connect to MySQL from my computer?

Mysql and java connection issue on ubuntu communication link failure

Here is the scenario
I am trying to connect to mysql database server from the core java app using spring. My application and server runs on different box i.e. Have limited ports open
Here is what I have done soo far
I am able to telnet to port 5506 (on which my database server run ) from the client machine
I am able to connect to database server from command prompt(mysql client) from client machine
3.I tried all driver version from 5.0.8 to latest
and off course same code works on everywhere except production :)
Somehow I started thinking that Java process required more ports to be open
Here is the exception
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused
STACKTRACE:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at java.net.Socket.<init>(Socket.java:392)
at java.net.Socket.<init>(Socket.java:235)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:169)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:381)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:455)
at org.springframework.jdbc.core.JdbcTemplate.queryForRowSet(JdbcTemplate.java:498)
at
Have you specified the 5506 port in the connection URL? This port is not the default one for MySQL (3306).

Categories