증명: servlet에 데이터베이스 접근의 병렬 문제가 존재할 수 있습니다

923 단어 servlet
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Session session = HibernateSessionFactory.getSession();
		Transaction t = session.beginTransaction();
		List list = session.createQuery(
				"from Student s where s.studentName='FrenchMan'").list();
		synchronized (lock) {
			if (list.size() == 0) {
				if (i++ % 2 == 0) {//i     ,             
					try {
						lock.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					lock.notify();
				}
				Student s = new Student();
				s.setStudentName("FrenchMan");
				session.save(s);
			}
		}
		t.commit();
		session.close();
	}

좋은 웹페이지 즐겨찾기