Search code examples
javajpaeclipselinkentitymanagerpool

Unable to create EvictionPolicy instance of type org.apache.commons.pool2.impl.DefaultEvictionPolicy


I'm getting below error in one part of my application where it tries to save/create/update data in the database. The application works fine for some time lets say close to 1-2 hours after deployment but starts giving this error on update calls after some time. I'm using JPA EclipseLink implementation.

Note: The update call is being done asynchronously using completable future. The call looks something like this :

CompletableFuture.supplyAsync(() -> {
            if (someBean != null) {
                try {
                    someReturnVal = methodCall();
                } catch (Exception e) {
                    log.info("Log with reason: "
                            + e.getMessage());
                }

            }
            return someReturnVal;
        }).thenAccept(someReturnVal -> {
            try {
                SomeBean.saveToDB(someReturnVal);
            } catch (AccessException e) {
                log.info("log with reason: " + e.getMessage());
            }
        });

SaveToDB looks like:

@Transactional
    public void SaveToDB(SomeBean arg1) {

        try {
            em.persist(arg1);
            em.flush();
        }Catch{
....
} 

Error message in stack trace:

     java.lang.IllegalArgumentException: Unable to create EvictionPolicy instance of type org.apache.commons.pool2.impl.DefaultEvictionPolicy
        at org.apache.commons.pool2.impl.BaseGenericObjectPool.setEvictionPolicyClassName(BaseGenericObjectPool.java:607)
        at org.apache.commons.pool2.impl.GenericKeyedObjectPool.setConfig(GenericKeyedObjectPool.java:257)
        at org.apache.commons.pool2.impl.GenericKeyedObjectPool.<init>(GenericKeyedObjectPool.java:111)
        at com.**.cloud.runtime.kotyo.persistence.client.pool.GenericKeyedObjectPoolAdapter.<init>(GenericKeyedObjectPoolAdapter.java:25)
        at com.**.cloud.runtime.kotyo.persistence.client.pool.managed.PoolableManagedConnectionFactoryAdapter.makeObject(PoolableManagedConnectionFactoryAdapter.java:86)
        at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:861)
        at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)
        at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)...
...
Caused by: java.lang.ClassNotFoundException: org/apache/commons/pool2/impl/DefaultEvictionPolicy
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forNameFW(Class.java:362)
    at java.lang.Class.forName(Class.java:355)
    at org.apache.commons.pool2.impl.BaseGenericObjectPool.setEvictionPolicyClassName(BaseGenericObjectPool.java:598)
    ... 77 common frames omitted

I'm clueless what is happening wrong here. Can someone please help me here.


Solution

  • I was able to resolve this issue by creating my own ThreadPoolExecutor using Executors, as below:

    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    

    I then passed this executor to my CompletableFuture call:

    CompletableFuture.supplyAsync(() -> { ....
                return someReturnVal;
            }, executor).thenAccept(someReturnVal -> {
                ....
            });
    

    this solved my problem. The issue seems to be when new CompletableFuture is asynchronously completed by a task running in the ForkJoinPool.commonPool().