I'm using MyBatis with playframework and everything works nicely except:
When I've attempted to use the default cache built in MyBatis (<cache/>
tag in xml mapper files), upon retrieving a cached select statement I get this stacktrace.
If I run the same statements in a unit test (or another application without playframework involved) everything works fine.
So, my guess it has something to do the way playframework loads/compiles the classes some sort of classloader issue.
The directory layout of my project looks something like this (relevant bits):
app
controllers
data // xml mapping files and java interfaces for ibatis
// & a class that returns a SqlSessionFactory
domain // Pojos for SQL results (e.g. User.java)
models
views
lib // here is myibatis jar file
(...) // the rest of the standard play framework project
The model method that throws the exception is this:
public static UserAuthentication findAuthenticatonIdentity(UserId userId){
SqlSessionFactory sessionFactory = IbatisSessionFactory.getSqlMapper();
SqlSession session = sessionFactory.openSession();
AuthMapper authMapper = session.getMapper(AuthMapper.class);
UserAuthentication ua = authMapper.selectByUserId(userId); // line 162
session.close();
return ua;
}
And UserAuthentication implements Serializable!
And here is the stacktrace:
Execution exception (In /app/models/UserModel.java around line 162)
PersistenceException occured :
### Error querying database. Cause: org.apache.ibatis.cache.CacheException: Error deserializing object. Cause: java.lang.ClassNotFoundException: data.domain.UserAuthentication
### Cause: org.apache.ibatis.cache.CacheException: Error deserializing object. Cause: java.lang.ClassNotFoundException: data.domain.UserAuthentication
play.exceptions.JavaExecutionException:
### Error querying database. Cause: org.apache.ibatis.cache.CacheException: Error deserializing object. Cause: java.lang.ClassNotFoundException: data.domain.UserAuthentication
### Cause: org.apache.ibatis.cache.CacheException: Error deserializing object. Cause: java.lang.ClassNotFoundException: data.domain.UserAuthentication
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:231)
at Invocation.HTTP Request(Play!)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.cache.CacheException: Error deserializing object. Cause: java.lang.ClassNotFoundException: data.domain.UserAuthentication
### Cause: org.apache.ibatis.cache.CacheException: Error deserializing object. Cause: java.lang.ClassNotFoundException: data.domain.UserAuthentication
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:94)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:58)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:90)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
at $Proxy8.selectByUserId(Unknown Source)
at models.UserModel.findAuthenticatonIdentity(UserModel.java:162)
at controllers.securesocial.MySecureSocial.checkAccess(MySecureSocial.java:97)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:504)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:478)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:473)
at play.mvc.ActionInvoker.handleBefores(ActionInvoker.java:322)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:142)
... 1 more
Caused by: org.apache.ibatis.cache.CacheException: Error deserializing object. Cause: java.lang.ClassNotFoundException: data.domain.UserAuthentication
at org.apache.ibatis.cache.decorators.SerializedCache.deserialize(SerializedCache.java:94)
at org.apache.ibatis.cache.decorators.SerializedCache.getObject(SerializedCache.java:50)
at org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:50)
at org.apache.ibatis.cache.decorators.SynchronizedCache.getObject(SynchronizedCache.java:55)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:72)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:100)
... 13 more
Caused by: java.lang.ClassNotFoundException: data.domain.UserAuthentication
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:603)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1574)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at java.util.ArrayList.readObject(ArrayList.java:593)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1848)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at org.apache.ibatis.cache.decorators.SerializedCache.deserialize(SerializedCache.java:91)
... 18 more
UserAuthentication.java
public class UserAuthentication implements Serializable {
private int id;
private String authId;
private String secret;
private String token; // Oauth1 token
private String accessToken; // Oauth2 request token
private String displayName;
private String authMethod;
private String provider;
private String email;
private String avatarUrl;
private Date lastAccess;
private String password;
private boolean emailVerified;
private int userId;
private String activationUuid;
private Date activationExpiry;
// + getters & setters
}
UserId.java
public class UserId implements java.io.Serializable {
/**
* The id the user has in a external service.
*/
public String id;
/**
* The provider this user belongs to.
*/
public ProviderType provider; // simple enum
}
Sorry, I think I was wrong - this is not a name collision but rather a classloader problem.
Play uses a dynamic classloading mechanism to detect changes to the source code and enhance methods through bytecode injection. http://groups.google.com/group/play-framework/browse_frm/thread/359736394ae27902/98445c557833036c
I think the problem may be that MyBatis is using the regular class loader - so can't find the classes that are loaded via Play's mechanism. When you put the source into the Play directory, MyBatis is then using the Play classloader.
Here are some options:
1) Compile your domain objects into a JAR file. Include that jar in your project: http://groups.google.com/group/play-framework/browse_thread/thread/b54e4e25ae49161b
2) Force MyBatis to use the Play classloader - somewhere before any mybatis calls
Thread.currentThread().setContextClassLoader(Play.classloader);
Still seems like a hack, but perhaps it keeps your application source size down.
3) Ensure your classes are loaded before the calls to MyBatis. So, in this case.
Class.forName("data.domain.UserAuthentication");
Class.forName("data.domain.UserId");
Which seems far worse of a hack.
I hope one of those options works for you.