I recently updated my MySQL server to MariaDB, since then I'm getting in trouble with a few errors.
After some time, my application crashes and these errors appear:
java.util.NoSuchElementException: Timeout waiting for idle object
or
Cannot get a connection, pool error Timeout waiting for idle object
Or NullPointerExceptions on executeUpdate() on a preparedStatement!
My code to connect to the database is:
public static void connect() {
try {
connection = DriverManager.getConnection("jdbc:mysql://"
+ Data.MySQL_host + ":3306/" + Data.MySQL_db,
Data.MySQL_user, Data.MySQL_pass);
System.out.println("MySQL connected!");
} catch (SQLException e) {
System.out.println("Error connecting to MySQL");
e.printStackTrace();
}
}
Do I have to modify my MariaDB Server, or is it application-related?
something wrong wiht your configuration,so you can't get a idle Object to connect.chekout your configuration first!
Related
Heading ##I have problem with my java application with database in mySQL and swing GUI.
When I've used localhost everything worked properly. But now I would like to share project with my friend and we decided to use server hosting.
And here is a problem:
Now application works very slow, after pressing a button I have to wait a few seconds for the program to respond. Also the connection is lost from time to time. I have no idea where can I find reason for the problem... Do somebody now what is the reason of this problem?
private static Connection connection = null;
Boolean error = false;
private static String jdbcURL = "jdbc:mysql://host_name:3306/db_name";
private static String user = "user";
private static String password = "password";
MakeConnection(Connection connection) throws SQLException{
this.connection = connection;
try {
getConnection();
System.out.print("Connected to the data base in MakeConnection");
}
catch(Exception e) {
error = true;
System.out.print("Error in connection in MakeConnection consturctor" + e.getMessage());
}
finally{
if(connection!=null) connection.close();
if(error) System.out.print("Problem with connection");
else System.out.print("Program finished");
}
}
public Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(jdbcURL,user,password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
Also sometimes application shows this error:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I don't see any problem in your code. The problem is probably with your server hosting. You should check the country of the host provider and measure the time required to send a request to the server. Also you should use logger instead of System.out.println so you can examine required time for actions like db access, application logic and find a bottleneck.
i have a database for practice at phpMyAdmin. i use XAMPP. I am in very early stages of learing about the conection with a database. I followa tutorial and i think i am understanding the concept and everything is cool. But i stepped on a problem that despite the fact that there are answers on the internet, i cant solve it. here is my code:
import java.sql.*;
public class DBConnect {
private Connection connection;
private Statement statement;
private ResultSet result;
public DBConnect(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:1234/practicedb"); //code stucks here and after some minutes it is throwing an exception
System.out.println("Connected");//this is never executed.
statement = connection.createStatement();
} catch (Exception ex) {
System.out.print("Error in Constractor: "+ex);
}
}
public void getData() {
try {
String query = "select * from cars";
result = statement.executeQuery(query);
while (result.next()) {
String name = result.getString("carName");
String id = result.getString("carID");
String modelNum = result.getString("modelNumber");
System.out.println("Car name: " + name + ", " + "Car ID: " + id + ", " + "Car model number: " + modelNum);
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
In the main class i create an instance of the class and then call the getData().
The code stucks in that line:
connection = DriverManager.getConnection("jdbc:mysql://localhost:1234/practicedb");
And it throws that:
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.java.lang.NullPointerException
This similar question was answered here: answer
But the suggestions are poor. I have tried flushing dns. I checked the URL and this is the one i connect to the database on phpmyadmin. i changed localhost to my ip adress. but all those just dont work.
I would really appreciate help. Is the first step on managing to receive that knowledge and i actually cant proceed at the moment. Thank you :)
i noticed that if i change the localhost:1234 to a random one like localhost:5432 it is throwing the error immediatelly. But when i have it on 1234(which i the port i have choosen through xampp config) then the programm runs for round about 5 minutes before it got terminated with the exception
Usually MySQL listens on the default port 3306.
If your database is named practicedb then your code should look like this:
private Connection connection;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/practicedb");
System.out.println("Connected");
} catch (Exception ex) {
System.out.print("Error creating connection: "+ex);
}
I have a big problem and I do not know how to fix it:
I have a singleton instance of the database as follow:
public Connection getConnection() throws SQLException {
if (db_con == null)
db_con = createConnection();
return db_con;
}
And I have a code that as follow:
shortTextScoringComponent.scoreComponent( "RS",SelectDataBase.getBlogs(rightSarcastic));
shortTextScoringComponent.scoreComponent( "RNS",SelectDataBase.getBlogs(rightNonSarcasm));
shortTextScoringComponent.scoreComponent( "FNS",SelectDataBase.getBlogs(wrongNonSarcasm));
shortTextScoringComponent.scoreComponent( "FS",SelectDataBase.getBlogs(wrongSarcasm));
So as you can see I call the database 4 times and it is note worthy that between each call there is a long time of processing so after he second line is performed successfuly I mean this line:
SelectDataBase.getBlogs(rightNonSarcasm);
when it comes to the third line I get the following error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 79,547,948 milliseconds ago. The last packet sent successfully to the server was 79,547,964 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
I searched a lot but there are many different answers which confuses me, do you have any idea what my exact problem is?
First of all as the exception says do add
autoReconnect=true
in your connectionString also add this as well
tcpKeepAlive=true
Secondly you can keep a polling thread to keep checking connection activeness
class PollingThread extends Thread
{
private java.sql.Connection connection;
PollingThread(int index, java.sql.Connection connection)
{
super();
this.connection = connection;
}
public void run()
{
Statement stmt = null;
while (!interrupted())
{
try
{
sleep(15 * 60 * 1000L);
stmt = connection.createStatement();
stmt.execute("do 1");
}
catch (InterruptedException e)
{
break;
}
catch (Exception sqle)
{
/* This thread should run even on DB error. If autoReconnect is true,
* the connection will reconnect when the DB comes back up. */
}
finally
{
if (stmt != null)
{
try
{
stmt.close();
} catch (Exception e)
{}
}
stmt = null;
}
}
Hello all I have a basic Storm application set up where it receives a stream of tweets and stores them in a MySQL database. The application works great for the first ~23 hours or so then it starts giving the following error:
SQL Exception
SQL State: 08003
After it does this a few times it dies. I'm using the standard JBDC connector to connect to the database from Java. The code for the functions for storing and setting up the DB connection are as follows:
private String _db="";
private Connection conn = null;
private PreparedStatement pst = null;
public ArchiveBolt(String db){
_db = db;
}
private void setupConnection() {
//Connect to the database
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:8889/twitter_recording", "root", "root");
} catch (Exception e){
e.printStackTrace();
}
}
public void execute(Tuple tuple, BasicOutputCollector collector) {
Status s = (Status) tuple.getValue(0);
//setup the connection on the first run through or if the connection got closed down
try {
setupConnection();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.toString());
}
try {
pst = conn.prepareStatement("INSERT INTO " + _db + " (tweet)" +
"VALUES (?);");
pst.setString(1, s.toString());
//execute the SQL
pst.executeUpdate();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
if(ex.getSQLState().equals("08003")){
setupConnection();
}
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
After it became apparent that it was crashing because of a 08003 error I decided that if it threw that error it should retry the set up of the connection, however that didn't help either. Could anyone point me in the right direction for solving this issue?
After it became apparent that it was crashing because of a 08003 error I decided that if it threw that error it should retry the set up of the connection, however that didn't help either. Could anyone point me in the right direction for solving this issue?
There are basically two problems here that need to be solved:
Why are the connections getting lost in the first place?
Why isn't your attempt to reconnect succeeding?
For the first problem, you should take a look at the MySQL logs to see if there are any indications there. And also, check for SQL exceptions immediately prior to the (repeated) "state 080003" exceptions. The latter are simply telling you that the connection has died previously.
My guess is that the problem is one of the following:
The MySQL server has timed out the connection due to inactivity. You can change the connection timeout in the MySQL configs if this is the problem.
Your application may be slowly leaking JDBC connections.
For the second problem, the general approach is correct, but your code doesn't match the description. In fact, it looks like it is always trying to establish a new database connection each time your execute method is called. This renders the reconnection call in your exception handler pointless. (OTOH, the code show signs that someone has been "beating on it" to try to get it to work ... and that could well be part of the problem.)
I would check that setupConnection is being called when it needs to be, and look for any exception that might be thrown. In addition, you should make sure that you explicitly close() the dead connection object ... and rethink / recode your connection management so that it doesn't leak.
For the record, there is a connection URL parameter called "autoReconnect" that in the distant past used to "deal" with lost connections. Unfortunately, the original implementation was unsafe, so they effectively disabled it; see this Question for details: Why does autoReconnect=true not seem to work?
I have main data source for working with database, which uses some connection pool. Now I want to create a second data source, which would use the same connection pool to perform logging operations in a separate transaction, then main database transaction! As far as I understood from the Glassfish docs if multiple data sources are using the same connection pool, then they share a transaction until connection is not closed (I might be wrong, please correct me).
So, is there a way to start a new transaction, when getting a connection to a data source? By setting TransactionIsolation may be?
Connection is retrieved in the following way:
private synchronized Connection getConnection() {
if (connection == null) {
try {
final Context ctx = new InitialContext();
final DataSource ds = (DataSource) ctx.lookup(getDataSourceLookupAddress());
connection = ds.getConnection();
} catch (final NamingException e) {
errorHandler.error("Datasource JNDI lookup failed: " + dataSourceLookupAddress + "!");
errorHandler.error(e.toString());
} catch (final SQLException e) {
errorHandler.error("Sql connection failed to " + dataSourceLookupAddress + "!");
errorHandler.error(e.toString());
}
}
return connection;
}
I have figured it out. It's necessary to start a new Thread, then a new transaction will be created. See also Writing to database log in the middle of rollback