0

In my java web app, I'm using a set of web services to query a db and jdbc.

When using a getter web service (select) it works fine, but when I use a post (insert), I am getting this error:

Lock wait timeout exceeded; try restarting transaction

For managing the db I am using this class

...
public final class MysqlConnect {
    public Connection conn;
    private Statement statement;
    public static MysqlConnect db;
    private MysqlConnect() {
           ...
        try {
            Class.forName(driver).newInstance();
            this.conn = (Connection)DriverManager.getConnection(url+dbName,userName,password);
        }
        catch (Exception sqle) {
            sqle.printStackTrace();
        }
    }

    public static synchronized MysqlConnect getDbCon() {
        if ( db == null ) {
            db = new MysqlConnect();
        }
        return db;

    }
    public ResultSet query(String query) throws SQLException{
        statement = db.conn.createStatement();
        ResultSet res = statement.executeQuery(query);
        ResultSetMetaData rsmd = res.getMetaData();
        int columnsNumber = rsmd.getColumnCount();
        while(res.next()) {
            for (int i = 1; i <= columnsNumber; i++) {
                if (i > 1) System.out.print(",  ");
                String columnValue = res.getString(i);
                System.out.print(columnValue + " ");
            }
            System.out.println("");
        }
        return res;
    }
 ...
    public int insert(String insertQuery) throws SQLException {
        statement = db.conn.createStatement();
        int result = statement.executeUpdate(insertQuery);
         return result;

    }

}

and here is my WS

@POST
@Path("/postMembership")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON+ ";charset=utf-8")
public Response postMembership(String MembershipRequest) throws JsonParseException, JsonMappingException, IOException, NamingException{     
    try {
        MysqlConnect.getDbCon().insert("INSERT INTO redmine.members (id, user_id, project_id, mail_notification) VALUES (301, 99, 99, 0)");
    } catch(Exception ex) {
        System.out.println("Exception getMessage: " + ex.getMessage());
    }
    return Response.status(200).entity("postMembership is called").build();
}   

I am using this DB locally so I am the only one in using it,

the same transaction works with mysqlworkbench.

How to get rid of it?

eeadev
  • 3,662
  • 8
  • 47
  • 100
  • This might be related to this problem [avoid lock wait timeout](http://stackoverflow.com/questions/13966467/how-to-avoid-lock-wait-timeout-exceeded-exception) – caburse Feb 12 '16 at 16:20

1 Answers1

-1

Here are some suggestions:

  1. Lock wait timeout’ occurs typically when a transaction is waiting on row(s) of data to update which is already been locked by some other transaction.
  2. Most of the times, the problem lies on the database side. The possible causes may be a inappropriate table design, large amount of data, constraints etc.
  3. Please check out this elaborate answer .
Community
  • 1
  • 1
CodeIsLife
  • 1,205
  • 8
  • 14