Good day,
Know that this title is all around stack overflow and there is multiple solution as well.
But there is a bit different in this case, which I not sure where should I continue to troubleshoot.
In my BaseUser.java
entity class, I have the following code:
@SuppressWarnings("serial")
@MappedSuperclass
public abstract class BaseUser< T extends BasePasswordHistory > extends
AuditModel implements IDeletable, IEditable, UserProfile {
// some other code here...
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "user")
@OrderBy("passwordHistoryId desc")
public Set< T > getPasswordHistories() {
return passwordHistories;
}
// some other code here...
}
And the following is part of the code I call the password history in ManagerImpl
level, BaseUserManagerImpl.java
.
Set< T > histories = user.getPasswordHistories( );
for ( T history : histories ) { // exception at this line in another environment
}
The BaseUserManagerImpl.java
is still under the same hibernate transaction, thus, it is able to load the passwordHistory
object even its lazy fetch
type. (Please correct me if I am wrong)
These code is working fine in the original environment (lets name it env A
) , until I deploy the same code into another environment (lets name it env B
), and it will hit LazyInitializationException
like title.
I check for the WAS version, found that the version have some different.
Actually I dun think these is the reason to hit the exception. But I really have no idea why the same code not working in different environment. Any point I should check further?
Here I post my stacktrace:
2019-06-25 17:17:47.967 [WebContainer : 3] ERROR c.c.i.c.c.u.e.ExceptionHandler - [cvmaker123] - Exception occurred
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.cv.ibs.cib.support.entity.BankUser.passwordHistories, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) ~[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) ~[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) ~[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108) ~[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186) ~[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at com.cv.ibs.cib.common.service.impl.BaseUserManagerImpl.verifyPasswordHistory(BaseUserManagerImpl.java:241) ~[com.cv.ibs.cib.jar:na]
at com.cv.ibs.cib.support.service.impl.BankUserManagerImpl.updatePassword(BankUserManagerImpl.java:288) ~[com.cv.ibs.cib.jar:na]
at com.cv.ibs.cib.support.service.impl.BankUserManagerImpl.updatePassword(BankUserManagerImpl.java:1) ~[com.cv.ibs.cib.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95) ~[na:1.7.0]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56) ~[na:1.7.0]
at java.lang.reflect.Method.invoke(Method.java:620) ~[na:2.6 (10-05-2016)]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) ~[org.springframework.aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) ~[org.springframework.aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) ~[org.springframework.aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) ~[org.springframework.aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[org.springframework.aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[org.springframework.aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at com.sun.proxy.$Proxy464.updatePassword(Unknown Source) ~[na:na]
at c.c.i.c.app.profile.command.ChangePasswordCommandHandler.execute(ChangePasswordCommandHandler.java:42) ~[com.cv.ibs.cib.jar:na]
And the following is part of the code:
public class ChangePasswordCommandHandler implements
CommandHandler< ChangePasswordCommand, Integer > {
@Autowired
private UserManager userManager;
public Integer execute(ChangePasswordCommand command)
throws BaseException {
return userManager.updatePassword( user, command.getNewPassword( ),
command.isResetPWD( ) );
}
}
@Service
public class UserManagerImpl extends
BaseUserManagerImpl< User, PasswordHistory > implements
UserManager {
public int updatePassword(User user, String newPassword,
boolean isResetPWD) throws BaseException {
User entity = findById( user.getUserId( ) );
final String newHash = getNewPasswordHash( entity, newPassword );
entity = verifyPasswordHistory( entity, entity.getPassword( ),
newHash, isResetPWD );
return 1;
}
}
public abstract class BaseUserManagerImpl< T extends BaseUser< U >, U extends BasePasswordHistory >
extends BaseManagerImpl< T, Long > implements BaseUserManager< T, U >,
UserProfileService {
final protected T verifyPasswordHistory(final T user, String oldHash,
String newHash, boolean isResetPWD) {
final int reuseNo = getMaxPassowrdhistory( );
Set< U > histories = user.getPasswordHistories( );
for ( U history : histories ) { // exception at this line in another environment
}
}
}
Kindly advise.
Finally I found what is wrong for this case. The WAS Java SDK for this project is pointing to Java 1.7. However, the code is compile using Java 1.6.
I found this when I check the WAS classloader
directory. After change Java SDK in WAS Console to Java 1.7, the problem solved.