Java 试图锁定表时超时;为什么会发生这种情况?我该如何解决?
我在Play框架中使用JPA已经有一段时间了,一切都很顺利——然而,我现在遇到了一个错误,我没有看到任何明显的解决方案。就某些方面而言,我试图创建的是一个基本的社交网络 我有一门博士后课程:Java 试图锁定表时超时;为什么会发生这种情况?我该如何解决?,java,database,jpa,playframework,Java,Database,Jpa,Playframework,我在Play框架中使用JPA已经有一段时间了,一切都很顺利——然而,我现在遇到了一个错误,我没有看到任何明显的解决方案。就某些方面而言,我试图创建的是一个基本的社交网络 我有一门博士后课程: public class Post extends Model { private String owner; private long timestamp; @ElementCollection private List<String> viewers; private String co
public class Post extends Model {
private String owner;
private long timestamp;
@ElementCollection
private List<String> viewers;
private String content;
public Post(String owner, List<String> viewers, String content) {
this.timestamp = System.currentTimeMillis();
this.owner = owner;
this.viewers = viewers;
this.content = content;
System.out.println("Saving post by " + owner + " with timestamp:" + this.timestamp);
}
(Getters and setters ignored here)
}
我使用3个线程的线程池运行我的应用程序,这意味着系统中存在一定程度的并发性。当系统在收到通知(executePost结束)后等待服务器响应时,另一个线程正在尝试使用以下代码访问新创建的Post:
public static void retrievePost(String owner, String timestamp) {
byte[] postAndKey = new byte[1024];
byte[] post = null;
byte[] encryptedKey = null;
User u = User.connect(owner);
Post.findAll();
//List<Post> posts = (Post.find("byOwner", owner).fetch());
System.out.println("Looking for post by " + owner + " at timestamp: " + timestamp);
//System.out.println("Looking through: " + posts.size() + " posts");
publicstaticvoidretrievepost(字符串所有者,字符串时间戳){
字节[]postAndKey=新字节[1024];
字节[]post=null;
字节[]encryptedKey=null;
用户u=用户连接(所有者);
Post.findAll();
//List posts=(Post.find(“byOwner”,owner.fetch());
System.out.println(“在时间戳“+时间戳”处由“+所有者+”查找帖子);
//System.out.println(“浏览:“+posts.size()+”posts”);
在Post.findAll()
中,框架抛出了一个严重错误,告诉我尝试锁定表“Post”时出现了超时。我怀疑这是因为一个线程仍在executePost()
中,而另一个线程正在retrievePost()中尝试访问Post
。但是,考虑到帖子已“保存”,锁不应该被释放吗?这真的是原因吗?是否有任何方法可以避免此错误
谢谢。如果其他人有类似的问题,请参考:我通过使用await()
显式休眠调用线程来修复它,这意味着它放弃了所有锁,允许线程进入retrievePost())
访问该表。仅供参考,如果其他人有类似问题:我通过使用await()
显式休眠调用线程来修复它,这意味着它放弃了所有锁,允许线程进入retrievePost()
进入表格。我对Play和JPA了解不多,但第一步是在executePost
、newPost.save();
和Post.findAll()周围放置一些日志语句要确认你的假设,我对Play和JPA不太了解,但第一步是在executePost
、newPost.save();
和Post.findAll();
周围放置一些日志语句来确认你的假设
public static void executePost(String content, String viewers) {
System.out.println("Post content: " + content);
String user = session.get("username");
User u = User.connect(user);
if (u == null) {
System.out.println("User is null");
}
/* Add post to local record of posts */
long timestamp = u.addPost(viewers, content);
/* Send notification of post to server */
}
public static void retrievePost(String owner, String timestamp) {
byte[] postAndKey = new byte[1024];
byte[] post = null;
byte[] encryptedKey = null;
User u = User.connect(owner);
Post.findAll();
//List<Post> posts = (Post.find("byOwner", owner).fetch());
System.out.println("Looking for post by " + owner + " at timestamp: " + timestamp);
//System.out.println("Looking through: " + posts.size() + " posts");