Hibernate + EhCache = java.io.NotSerializableException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3395042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:38:12  来源:igfitidea点击:

Hibernate + EhCache = java.io.NotSerializableException

javahibernategrailsehcache

提问by Oleksandr

I'm doing load test now. After approximate 900 requests i got this error:

我现在在做负载测试。大约 900 个请求后,我收到此错误:

[ 03.08.10 11:49:00.465] [Store org.hibernate.cache.StandardQueryCache Spool Thread] ERROR net.sf.ehcache.store.DiskStore  - org.hibernate.cache.StandardQueryCacheCache: Failed to write element to disk 'sql: select hotelfeatu0_.name as col_0_0_ from feature hotelfeatu0_ where hotelfeatu0_.class='org.company.domains.HotelFeatures' and hotelfeatu0_.lang=? and hotelfeatu0_.hotel_id=?; parameters: ; named parameters: {ht=100: 'Hotel', lng=en}'. Initial cause was org.company.domains.Hotel
java.io.NotSerializableException: org.company.domains.Hotel
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    at java.util.HashMap.writeObject(HashMap.java:1001)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:416)
    at net.sf.ehcache.Element.writeObject(Element.java:729)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    at net.sf.ehcache.util.MemoryEfficientByteArrayOutputStream.serialize(MemoryEfficientByteArrayOutputStream.java:75)
    at net.sf.ehcache.store.DiskStore.serializeElement(DiskStore.java:781)
    at net.sf.ehcache.store.DiskStore.writeElement(DiskStore.java:735)
    at net.sf.ehcache.store.DiskStore.writeOrReplaceEntry(DiskStore.java:729)
    at net.sf.ehcache.store.DiskStore.flushSpool(DiskStore.java:703)
    at net.sf.ehcache.store.DiskStore.throwableSafeFlushSpoolIfRequired(DiskStore.java:671)
    at net.sf.ehcache.store.DiskStore.spoolAndExpiryThreadMain(DiskStore.java:640)
    at net.sf.ehcache.store.DiskStore.access0(DiskStore.java:68)
    at net.sf.ehcache.store.DiskStore$SpoolAndExpiryThread.run(DiskStore.java:1110)

Here is my ehcache.xml:

这是我的 ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false">

    <diskStore path="java.io.tmpdir/sweethome"/>

    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
    />

    <cache
                name="org.hibernate.cache.StandardQueryCache"
                maxElementsInMemory="5"
                eternal="false"
                timeToLiveSeconds="120"
                overflowToDisk="true"
    />

</ehcache>

Hibernate config:

休眠配置:

hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true
    cache.provider_class='org.hibernate.cache.EhCacheProvider'
    connection.useUnicode=true
    connection.characterEncoding='UTF-8'
    dialect = 'org.hibernate.dialect.MySQL5InnoDBDialect'
}

回答by waxwing

The log tells me that this happens because the query cache tries to write one of your org.company.domains.Hotelobjects on disk. Not surprising that this might happen during a load test, since you only allow 5 elements in the query cache in memory before it tries to flush it to disk.

日志告诉我发生这种情况是因为查询缓存尝试将您的org.company.domains.Hotel对象之一写入磁盘。这在负载测试期间可能发生并不奇怪,因为在尝试将其刷新到磁盘之前,您只允许内存中的查询缓存中有 5 个元素。

You either need to make org.company.domains.Hotelimplement java.io.Serializable, or set overflowToDisk="false"for the query cache.

您要么需要org.company.domains.Hotel实现java.io.Serializable,要么overflowToDisk="false"为查询缓存设置。