简体   繁体   中英

Lock wait timeout exceeded- insert using a singleton db manager

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM