编辑:代码已更新,包括@Igor Artamonov建议的更改,
然而问题仍然存在!我确实注意到以下reference to this image,所有者2有一个高度值,没有tool_proficiency值,直到添加了所有者3,然后2的高度值消失,2的工具值出现,2创建了smith的工具
编辑结束
这可能类似于“This Question”,但是我无法使用那里给出的解决方案解决问题而我无法发表评论,因为我的代表太少了:(
所以我希望你不介意我问我在这里的问题:) *注意如果有一个更好的地方让我提出我的问题或任何问题告诉我,我在这里很新:P
我的网页有一个java帮助程序,用于设置数据存储区中实体的属性,但每次至少丢失一个项目,如果不是更多的话?所有项目都使用相同的功能设置!
JSP页面上的代码:
String Username = (String)session.getAttribute("username");
System.out.println("Username carried from session variable = "+Username);
if(request.getParameter("alignment")!=null
&& request.getParameter("Size")!=null
&& request.getParameter("age")!=null
&& request.getParameter("tools")!=null){
String HD = "Hill Dwarf";
String Age = request.getParameter("age");
String Alignment = request.getParameter("alignment");
int Size = Integer.parseInt(request.getParameter("Size"));
int Rem = Size%12;
Size = (Size-(Size%12))/12;
String Height = (Size+"ft "+Rem+"in");
String Tools = request.getParameter("tools");
System.out.println("setString for hill dwarf: ");
login.setString(Username, "Race", HD);
System.out.println("setString for age: ");
login.setString(Username, "Age", Age);
System.out.println("setString for Alignment: ");
login.setString(Username, "Alignment", Alignment);
System.out.println("setString for height: ");
login.setString(Username, "Height", Height);
System.out.println("setString for tools: ");
login.setString(Username, "Tool_Proficiency", Tools);
System.out.println("redirecting to class page");
response.sendRedirect("class.jsp");
}
登录助手中的代码:
public void setString(String usernamein,String columnin, String stringin){
Transaction txn = datastore.beginTransaction();
try{
Filter usernamefilter = new FilterPredicate("owner",
FilterOperator.EQUAL, usernamein);
Query validuserquery = new Query("Char").setFilter(usernamefilter);
Entity theUser = datastore.prepare(validuserquery).asSingleEntity();
System.out.println("Username passed to setString = "+usernamein);
System.out.println("Column name passed to setString = "+columnin);
System.out.println("String passed to setString = "+stringin);
System.out.println("Query = "+validuserquery);
if (theUser==null && counter < 30){
System.out.println("theUser was equal to null");
try {
System.out.println("sleeping for 400ms");
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter ++;
System.out.println("counter increased, counter = "+counter);
System.out.println("Recursing function");
setString(usernamein,columnin,stringin);
}else{
System.out.println("theUser was not == null ");
System.out.println("inputting: "+columnin+" = "+stringin);
theUser.setProperty(columnin,stringin);
datastore.put(theUser);
System.out.println("item added");
}
txn.commit();
}finally{
if (txn.isActive()){
txn.rollback();
}
}
}
如果有人需要我解释代码,我只会问:)
好的,这是将Age,Alignement等放入数据存储区的代码,但它在数据存储区中提供了这个结果?我不为我的生活知道为什么!?
这可能是一张图片,但我需要10个代表,所以你必须follow this link for the image :(再次抱歉
这就是eclipse控制台从所有System.out.println()获取的内容
这不是代码,但我会把它放在一个代码片段中以便于阅读:)
HillDwarf page loaded
Username carried from session variable = 12
setString for hill dwarf:
Username passed to setString = 12
Column name passed to setString = Race
String passed to setString = Hill Dwarf
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Race = Hill Dwarf
item added
setString for age:
Username passed to setString = 12
Column name passed to setString = Age
String passed to setString = 10
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Age = 10
item added
setString for Alignment:
Username passed to setString = 12
Column name passed to setString = Alignment
String passed to setString = N
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Alignment = N
item added
setString for height:
Username passed to setString = 12
Column name passed to setString = Height
String passed to setString = 4ft 5in
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Height = 4ft 5in
item added
setString for tools:
Username passed to setString = 12
Column name passed to setString = Tool_Proficiency
String passed to setString = Brewer's Supplies
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Tool_Proficiency = Brewer's Supplies
item added
redirecting to class page
所以我在图像中看到的问题是数据存储区每次都没有得到所有项目,即使我每次都使用相同的帮助方法!
请帮助我一直尝试用几种方法解决这个问题:(
答案 0 :(得分:1)
您没有使用交易,因此有些写入会被其他人替换。
试试这个:
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try {
/// your current code
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
请参阅https://cloud.google.com/appengine/docs/java/datastore/transactions
PS你还应该明白,你当前的代码比在.put
中进行相同更新的代码贵4倍。对于每个领域,我都没有看到任何理由进行4次。
答案 1 :(得分:0)
我正在回答我自己的问题,因为我今天终于解决了问题:S哦,我也是一个愚蠢的人!
如果将来有任何人遇到此问题,请确保您正确使用密钥!
当Char被创建时我使用了这个:
Key Charkey = KeyFactory.createKey("owner","id");
Entity Char = new Entity("Char", Charkey);
在我的帮手中,我删除了交易(不幸的是,这对我来说不好用),并且 - 取代:
Filter usernamefilter = new FilterPredicate("owner", FilterOperator.EQUAL, usernamein);
Query validuserquery = new Query("Char").setFilter(usernamefilter);
- 带有:
Key Charkey = KeyFactory.createKey("owner","id");
Filter usernamefilter = new FilterPredicate("owner", FilterOperator.EQUAL, usernamein);
Query validuserquery = new Query("Char",Charkey).setFilter(usernamefilter);
相当愚蠢的错误花了我几个小时!我对数据存储区是如此新颖,所以我总是希望这些错误无论如何都希望在将来的某个时刻修复别人的问题:D