/**** FIX FOR HIBERNATE OBJECT */
if (PersistenceUtils.isProxy(value)){
if(!Hibernate.isInitialized(value)){
if (INFO)
info("kryo", "uninitialized hibernate proxy detected and initializing: " + this + " (" + object.getClass().getName() + ")" + " pos=" + output.position());
Hibernate.initialize(value);
}
value = PersistenceUtils.cleanFromProxies(value);
if (INFO)
info("kryo", "Write field detected hibernate proxy and unproxying: " + this + " (" + object.getClass().getName() + ")" + " pos=" + output.position());
}
/****END OF FIX *****/
kryoSerializer = new Kryo() {
@Override
public Serializer<?> getDefaultSerializer( final Class type ) {
if (AbstractPersistentCollection.class.isAssignableFrom( type )) {
return new FieldSerializer( kryoSerializer, type );
}
return super.getDefaultSerializer( type );
}
}
write
method of the serializer and unwrap the proxy before calling super.write
my take on this is:
public class KryoSerializer {
private static final KryoFactory factory = () -> {
Kryo kryo = new Kryo();
return kryo;
};
private static final KryoPool pool = new KryoPool.Builder(factory)
.softReferences()
.build();
public byte[] serialize(Object object) {
Output output = new Output(new ByteArrayOutputStream());
Kryo kryo = pool.borrow();
kryo.writeClassAndObject(output, object);
output.close();
pool.release(kryo);
return output.getBuffer();
}
public Object deserialize(byte[] byteArray) {
Input input = new Input(byteArray);
Kryo kryo = pool.borrow();
Object object = kryo.readClassAndObject(input);
input.close();
pool.release(kryo);
return object;
}
}
Do you see any problem with this implementation?
KryoPool
class is not included anymore. Couldn't find anything in migration guide either: https://github.com/EsotericSoftware/kryo/wiki/Migration-to-v5