Resolving Ehcache serialization errors using the Transient keyword

Source: Internet
Author: User
Tags serialization

When you use Ehcache, you find an obscure little problem.

The following code is in a model:

 Public class Implements Serializable {    privatestaticfinallong serialversionuid =- 990334519496260591L;     Private Iuserservice US = ServiceManager.me.getUserService ();     // Getter and Setter }

When you cache an instance of this model to Ehcache, you will be prompted with an error if Ehcache is configured to allow saving to disk:

ERROR diskstoragefactory Disk Write of xxxxxxx Failed:
java.io.notserializableexception:com.my.service.impl.userserviceimpl$ $EnhancerByGuice $$80ede2b6

Look at the error, it is clear that the Userserviceimpl object can not be serialized, it is ehcache the service logic also as the model of the normal properties

Under normal circumstances, the Userserviceimpl implementation of the Serializable interface can solve the problem

But in fact, this service is a stateless single-column pattern, it makes no sense to serialize it to disk, and it increases the IO overhead of the disk

How can you cache the model successfully and shaving the properties of these service logic?

At this time, an obscure keyword played a role:transient

Change the above code to the following, in fact, just add this keyword:

 Public class Implements Serializable {    privatestaticfinallong serialversionuid =- 990334519496260591L;     Private  iuserservice US = ServiceManager.me.getUserService ();     // Getter and Setter }

When you cache again, the error will not be prompted.

Here is an article on the Transient keyword is elaborated in more detail: https://www.cnblogs.com/lanxuezaipiao/p/3369962.html

Resolving Ehcache serialization errors using the Transient keyword

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.