# Confluence DC throws ClassCastException when getting object from Bandana after app upgrade

**URL:** <https://community.developer.atlassian.com/t/confluence-dc-throws-classcastexception-when-getting-object-from-bandana-after-app-upgrade/31894>\
**Category:** Confluence Data Center\
**Tags:** cache, confluence-plugin, caching, bandana, data-center\
**Created:** [September 16, 2019, 9:32am UTC](https://community.developer.atlassian.com/t/confluence-dc-throws-classcastexception-when-getting-object-from-bandana-after-app-upgrade/31894 "2019-09-16T09:32:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emre.toptanci](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@emre.toptanci](https://community.developer.atlassian.com/u/emre.toptanci)\
**Post date:** [September 16, 2019, 9:32am UTC](https://community.developer.atlassian.com/t/confluence-dc-throws-classcastexception-when-getting-object-from-bandana-after-app-upgrade/31894/1 "2019-09-16T09:32:10Z")

</div>

My app is keeping some objects in the Bandana table. (I know it is not best practice but wasn’t my choice). When I install a new package of my app I start getting ClassCastException while getting my objects via BandanaManager.getValue().

To be clear, BandanaManager.getValue() successfully returns an object but it can’t be cast to the desired type. While debugging I can clearly see that the class name (and even the hashcode) of the object I am getting from BandanaManager.getValue() and the class name that I am trying to cast to have the exact same Fully Qualified Name, yet I get an exception.

The package I am uploading does not have to be a new version of the app. Even uploading the same jar twice causes this problem. It happens only on DataCenter, even if it works as a single node, Confluence Server does not have this. I keep getting the exception until Confluence instance is restarted.

The issue reported below seems to be related to this. I guess it has something to do with how Bandana Caches work and which classloaders are used. Unlike the issue reported below, I am not creating and/or operating those caches, Confluence is.

> [@Cache in Data Center throws ClassCastException when casting to the same class](https://community.developer.atlassian.com/t/cache-in-data-center-throws-classcastexception-when-casting-to-the-same-class/3232):
>
> Hi guys, I’m trying to use cache in JIRA Data Center but I’m getting this error: java.lang.ClassCastException: com.softwareplant.util.lang.POJO cannot be cast to com.softwareplant.util.lang.POJO at com.softwareplant.datacenter.DataCenterService.getAllJIRAS(DataCenterService.java:67) This error occurs when I’m iterating through values in my cache: Collection\<String\> keys = jiraCache.getKeys(); for (String key : keys) { POJO pojoValue = jiraCache.get(key); log.info("\nKEY = {}, VALUE = {} "…

I don’t want to use reflection as a workaround for performance considerations and I also don’t want to de-serialize/re-serialize the object, also based on performance considerations.

Any possible workarounds are welcome.

---

<div class="post-metadata">

**Author:** ![emre.toptanci](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@emre.toptanci](https://community.developer.atlassian.com/u/emre.toptanci)\
**Post date:** [September 16, 2019, 10:18am UTC](https://community.developer.atlassian.com/t/confluence-dc-throws-classcastexception-when-getting-object-from-bandana-after-app-upgrade/31894/2 "2019-09-16T10:18:51Z")

</div>

We prepared the following sample Confuence App project that reproduces the problem.

> **[Dropbox - File Deleted](https://www.dropbox.com/s/bnvdejo7deuq9au/bandanaBugTest.rar?dl=0)**
>
> Dropbox is a free service that lets you bring your photos, docs, and videos anywhere and share them easily. Never email yourself a file again!

- Compile and install the app on a Confluence DC instance.
- Call the following endpoint which will save something to the Bandana table.  
\*\* \<confluenceUrl\>/rest/bandanaBugTest/latest/bandana/save/{SPACE\_KEY}?id={ID}&name={NAME}  
\*\* ID is an integer and NAME is a string.
- Call the following endpoint to confirm that you can read the object successfully.  
\*\* \<confluenceUrl\>/rest/bandanaBugTest/latest/bandana/get/{SPACE\_KEY}/{ID}
- Upload the same plugin jar file without changing (or even compiling) anything.
- Call the second endpoint (get endpoint) again to get the exception.

---

<div class="post-metadata">

**Author:** ![amoerchen](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/amoerchen/32/36650_2.png) [@amoerchen](https://community.developer.atlassian.com/u/amoerchen)\
**Post date:** [January 28, 2021, 11:13am UTC](https://community.developer.atlassian.com/t/confluence-dc-throws-classcastexception-when-getting-object-from-bandana-after-app-upgrade/31894/3 "2021-01-28T11:13:13Z")

</div>

Workaround: Serialize/deserialize on your own, and only store a String in Bandana.

---

<div class="post-metadata">

**Author:** ![computerpraxis](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@computerpraxis](https://community.developer.atlassian.com/u/computerpraxis)\
**Post date:** [September 19, 2021, 4:20pm UTC](https://community.developer.atlassian.com/t/confluence-dc-throws-classcastexception-when-getting-object-from-bandana-after-app-upgrade/31894/4 "2021-09-19T16:20:08Z")

</div>

HI!

I have the same problem with Confluence 7.13.0 DC.

Did You find a solution/workaround ?

regards,  
Hans

---

<div class="post-metadata">

**Author:** ![RobertKrause](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/robertkrause/32/40108_2.png) [@RobertKrause](https://community.developer.atlassian.com/u/RobertKrause)\
**Post date:** [September 24, 2021, 9:52am UTC](https://community.developer.atlassian.com/t/confluence-dc-throws-classcastexception-when-getting-object-from-bandana-after-app-upgrade/31894/5 "2021-09-24T09:52:24Z")

</div>

Hi,

the cause of this issue is different versions of the same object. When you install your app the first time, the jvm class loader will create an instance of the class. Class loaders can only access classes created by themself. Then an object of this class is created, persisted and added to a cache. Now you update the installed app, and install it again. The (new) class loader will create another version of the class. When accessing the object in the cache (createed by the old class loader) the ClassCastException is thrown, because the object was created by a different class loader. To avoid this, you can now clear the cache. Trying to access this object, which does not exist in the cache now, will create a new object (from the new class loader) and load the serialized object from the bandana, which will now be deserialized by the new class loader.

So the solution we frequently use is to add a event listener, that listens for the PluginEnabledEvent and clears our caches.
