出于好奇,这只是一个简单的理论问题.我一直都喜欢java风扇男孩.但有一件事让我想知道为什么java不提供在堆栈上创建对象的机制?如果我可以在堆栈而不是堆上创建小的Point(int x,int y)对象,就像在C#上创建结构一样,那会不会更有效率.在java中这个限制背后有什么特殊的安全原因吗?:)
所以,我正在开发一个简单的Spring MVC + JPA(hibernate)项目,其中有用户可以制作帖子并在他们的朋友帖子上发表评论(有点像一个小社交网络).使用JPA Hibernate我还是比较新的.因此,当我尝试从浏览器中测试一些任务(包含事务)的多个请求时,在处理上一个请求时非常快2-3次,我得到一个OptimisticLockException.这是堆栈跟踪..
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [org.facebookjpa.persistance.entity.Post] with identifier [19]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [org.facebookjpa.persistance.entity.Post#19]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
Run Code Online (Sandbox Code Playgroud)
现在,我该如何解决这个问题?当多个事务请求同时发生时,如何正确处理此ObjectOptimisticLockException?我应该遵循哪些好的模式?我需要使用某种悲观锁定机制吗?
这是我目前正在使用的DAO ..提前感谢.:)
@Repository
@Transactional
public class PostDAOImpl implements PostDAO {
@Autowired
UserDAO userDAO;
@Autowired
CommentDAO commentDAO;
@Autowired
LikeDAO likeDAO;
@PersistenceContext
private EntityManager entityManager;
public PostDAOImpl() {
}
@Override
public boolean insertPost(Post post) …Run Code Online (Sandbox Code Playgroud) 是否可以在不使用序列化的情况下对Java对象进行深层复制/克隆?如果是这样呢?
我试图在android上测试一个简单的文件i/o程序,我在EditText中键入一些文本,当我单击Save按钮时,它将内容写入文件.当我单击加载按钮时,它将内容加载回EditText.
这是我的代码---
public class Activity2 extends Activity {
private EditText textBox;
private static final int READ_BLOCK_SIZE = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
textBox = (EditText) findViewById(R.id.txtText1);
Button saveBtn = (Button) findViewById(R.id.btnSave);
Button loadBtn = (Button) findViewById(R.id.btnLoad);
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String str = textBox.getText().toString();
try
{
PrintWriter PR=new PrintWriter(new File("text1.txt"));
PR.write(str);
PR.flush();
PR.close();
//textBox.setText("");
//---display file saved message---
Toast.makeText(getBaseContext(),
"File saved successfully!",
Toast.LENGTH_SHORT).show(); …Run Code Online (Sandbox Code Playgroud) java ×3
android ×1
deep-copy ×1
file-io ×1
hibernate ×1
jpa ×1
stack-memory ×1
transactions ×1
value-type ×1