我一直在Play Framework中使用JPA一段時間了,一切都很順利 - 但是,現在我已經遇到了一個我沒有看到明顯解決方案的錯誤。僅僅在某些情況下,我想創建的是一個基本的社交網絡。超時嘗試鎖定表;爲什麼會發生這種情況,我該如何解決?
我有一個郵政類:
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)
}
我有一個用戶類,增加了帖子:
public long addPost(String viewers, String content) {
LinkedList<String> viewersList = new LinkedList(Arrays.asList(viewers.split(",")));
Post newPost = new Post(this.name, viewersList, content);
newPost.save();
return newPost.getTimestamp();
}
我有職位的職位的StreamManager處理通知和檢索。
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 */
}
我正在用3個線程的線程池運行我的應用程序,這意味着系統中存在一定的併發量。當系統正在等待通知(executePost結束)後,服務器的響應,另一個線程嘗試使用此代碼來訪問新創建的帖子:
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");
在Post.findAll()
框架拋出一個討厭的錯誤,告訴我有一個Timeout trying to lock table "POST"
。我懷疑這是因爲一個線程仍處於executePost()
而另一個線程正在嘗試訪問retrievePost()
中的帖子。考慮到郵政已經'救了',但是,鎖不應該被釋放?這真的是這個原因嗎?有什麼方法可以解決這個錯誤嗎?
謝謝。
我對Play和JPA瞭解不多,但第一步是在'executePost','newPost.save();'和'Post.findAll();'周圍放置一些日誌語句以確認您的假設 – 2012-02-16 17:56:33