0
當兩個線程一起工作時,我想要restirict用戶表更新,這引發了「行被更新或刪除的另一個事務(或未保存的值映射不正確):[com .dmainc.commons.security.User#4]「 。當第二個線程先擊中時開始回滾並拋出異常。 實際上每個線程都在修改passwordAttemptDate.I,每次用戶登錄時都需要更新這個字段,如何對這兩個線程同時執行這個操作。我在一個事務中調用這個方法。行被更新或刪除的另一個事務 - 多線程
任何建議都會得到滿足。
public AuthenticationStatus authenticateEncrypted(User user, String password, ISecurityParameters params,
Boolean encryptedPassword)
{
AuthenticationStatus status = new AuthenticationStatus();
status.setUser(user);
status.setStatus(AuthenticationStatus.Status.UNKNOWN_ERROR);
// hash password
String hashedPass = (encryptedPassword == true ? password : hashPassword(password, user.getSalt()));
// compare to stored password and check on attempts if failed
if(hashedPass.equals(user.getPassword()))
{
user.setPasswordAttempts(0);
//THIS IS GETTING UPDATED EACH TIME
user.setPasswordAttemptDate(Calendar.getInstance());
if(getPasswordNotify(user, params) >= 0)
{
status.setStatus(AuthenticationStatus.Status.SUCCESS_NOTIFY);
}
else if(isPasswordExpired(user, params))
{
status.setStatus(AuthenticationStatus.Status.PASSWORD_EXPIRED);
}
else
{
status.setStatus(AuthenticationStatus.Status.SUCCESS);
}
status.setArg(arg);
return status;
}
else
{
user.setPasswordAttempts((user.getPasswordAttempts() == null) ? 1 : user.getPasswordAttempts() + 1);
user.setPasswordAttemptDate(Calendar.getInstance());
status.setStatus(AuthenticationStatus.Status.FAIL);
}
}
catch(Exception pe)
{
if(LOG.isErrorEnabled())
LOG.error(pe, pe);
}
return status;
}
User.hbm.xml
<hibernate-mapping>
<class name="com.dmainc.commons.security.User" table="userdata">
<id name="userId" type="long">
<column name="UserId" />
<generator class="native" />
</id>
<version name="version" type="int">
<column name="Version" not-null="true" />
</version>
<property name="username" type="string">
<column name="Username" length="50" not-null="true" />
</property>
<property name="firstName" type="string">
<column name="FirstName" length="50" />
</property>
<property name="lastName" type="string">
<column name="LastName" length="50" />
</property>
<property name="email" type="string">
<column name="Email" length="50" />
</property>
<property name="status" type="string">
<column name="Status" length="2" />
</property>
<property name="passwordAttempts" type="int">
<column name="PasswordAttempts" />
</property>
<property name="passwordAttemptDate" type="calendar">
<column name="PasswordAttemptDate" length="19" />
<many-to-one name="organization" class="com.dmainc.commons.security.Organization" fetch="select">
<column name="OrganizationId" />
</many-to-one>
<set name="userpasswords" table="userpasswords" inverse="true" lazy="true" fetch="select" cascade="all,delete-orphan">
<key>
<column name="UserId" not-null="true" />
</key>
<one-to-many class="com.dmainc.commons.security.UserPassword" />
</set>
<bag name="userroles" table="userrole" inverse="true" lazy="false" fetch="select" cascade="all,delete-orphan">
<key>
<column name="UserId" not-null="true" />
</key>
<one-to-many class="com.dmainc.commons.security.UserRole" />
</bag>
</class>
你應該用那麼Session.lock(對象entitty,LockMode鎖)。通常,您要麼使用LockMode.PESSIMISTIC_WRITE要麼使用LockMode.UPGRADE_NOWAIT。後一個選項僅適用於支持select ..... for update no等語法的數據庫系統(例如Oracle,Sql服務器)。請注意,在更改鎖定模式時,您可能必須增加trasacaction超時) –
當我從hbm中刪除版本時它的工作正常,這可能是一個可能的解決方法嗎?將刪除它的影響是什麼?@dsp_user – Surya
可能是因爲Hibernate使用樂觀鎖定(https://www.intertech.com/Blog/versioning-optimistic-locking-in-hibernate/) –