Hibernate を使用して最初の Java アプリケーションを構築していますが、Hibernate セッションに少し問題があります。
私の問題: 2 番目のユーザーがアプリケーションにログインすると、最初のユーザーのセッションが上書きされます -> 両方が現在 2 番目のセッションで作業しています。ログイン時に両方のユーザーが新しいセッションを作成しますが。
私のコード:
最初に、ユーザーがログインしたときに現在のユーザーを取得します (LoginDetail.java):
userBean = UserProxy.getInstance().getElementByUser(userBean.getUser(), userBean.getPassword());
UserProxy.java:
public static synchronized UserProxy getInstance() {
if (instance == null) {
instance = new UserProxy();
}
return instance;
}
public UserBean getElementByUser(String user, String password) {
try {
Iterator<SUsers> iter = s.createQuery("from SUsers where user = '" + user + "' and password = '" + password + "'").list().iterator();
while (iter.hasNext()) {
userDB = iter.next();
currentUser = convertClassToBean(userDB);
log.debug("aktuell ausgewaehlter char: " + userDB.getId());
}
} catch (Exception e) {
log.error(e);
e.printStackTrace();
}
currentUser = convertClassToBean(userDB);
return currentUser;
}
私の HibernateUtil.java:
public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
public static Session currentSession() {
Session s = session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
および hibernate.cfg.xml:
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.isolation">2</property>
この問題を解決する方法を誰かに教えてもらえますか?