在我的java Web应用程序中,我使用一组Web服务来查询db和jdbc。
使用getter Web服务(选择)时,它工作正常,但当我使用帖子(插入)时,我收到此错误:
Lock wait timeout exceeded; try restarting transaction
管理数据库我正在使用这个类
...
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;
}
}
这是我的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();
}
我在本地使用这个数据库所以我是唯一使用它的人,
同一个事务与mysqlworkbench一起使用。
如何摆脱它?
答案 0 :(得分:-1)
以下是一些建议: