DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Better performing Glassfish JDBC Pool with non-blocking DataStructure

Sven Diedrichsen user avatar by
Sven Diedrichsen
·
Nov. 13, 14 · · Code Snippet
Like (0)
Save
Tweet
449 Views

Join the DZone community and get the full member experience.

Join For Free

The above DataStructure implementation allows concurrent threads to access the resource pool and thus provide a higher throuput. This performance gain is achieved on sacrificing some correctness of the size of the pool at any moment but allows a higher resource throughput. The maximum size of the pool will be assured nevertheless.

package com.sun.enterprise.resource.pool.datastructure;

import com.sun.appserv.connectors.internal.api.PoolingException;
import com.sun.enterprise.resource.ResourceHandle;
import com.sun.enterprise.resource.allocator.ResourceAllocator;
import com.sun.enterprise.resource.pool.ResourceHandler;
import com.sun.enterprise.resource.pool.datastructure.strategy.ResourceSelectionStrategy;
import com.sun.logging.LogDomains;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NonBlockingDataStructure implements DataStructure {

    private ResourceSelectionStrategy strategy;
    private final ResourceHandler handler;
    private final AtomicInteger maxSize;

    private final ConcurrentLinkedQueue freeResourceHandles = new ConcurrentLinkedQueue();
    private final ConcurrentLinkedQueue allResourceHandles = new ConcurrentLinkedQueue();

    protected final static Logger _logger =
            LogDomains.getLogger(NonBlockingDataStructure.class, LogDomains.RSR_LOGGER);

    public NonBlockingDataStructure(String parameters, int maxSize,
                                    ResourceHandler handler, String strategyClass) {
        this.maxSize = new AtomicInteger(maxSize);
        this.handler = handler;
        initializeStrategy(strategyClass);
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.log(Level.FINEST, "pool.datastructure.rwlockds.init");
        }
    }

    private void initializeStrategy(String strategyClass) {
        //TODO
    }

    @Override
    public void setMaxSize(int i) {
        this.maxSize.set(i);
    }

    @Override
    public int addResource(ResourceAllocator resourceAllocator, int count) throws PoolingException {
        if(_logger.isLoggable(Level.FINE)){
            _logger.fine("addResource for "+count+" resources.");
        }
        int addedResourceHandles = 0;
        try {
            for (int i = 0; i < count && allResourceHandles.size() < maxSize.get(); i++) {
                ResourceHandle handle = handler.createResource(resourceAllocator);
                allResourceHandles.offer(handle);
                freeResourceHandles.offer(handle);
                addedResourceHandles++;
            }
        } catch (Exception e) {
            PoolingException pe = new PoolingException(e.getMessage());
            pe.initCause(e);
            throw pe;
        }
        if(_logger.isLoggable(Level.FINE)){
            _logger.fine("addResource added "+addedResourceHandles+" resources.");
        }
        return addedResourceHandles;
    }

    @Override
    public ResourceHandle getResource() {
        ResourceHandle resourceHandle = freeResourceHandles.poll();
        if (resourceHandle != null) {
            resourceHandle.setBusy(true);
            if(_logger.isLoggable(Level.FINE)){
                _logger.fine("getResource found resource.");
            }
        }
        return resourceHandle;
    }

    @Override
    public void removeResource(ResourceHandle resourceHandle) {
        if(_logger.isLoggable(Level.FINE)){
            _logger.fine("removeResource called.");
        }
        allResourceHandles.remove(resourceHandle);
        freeResourceHandles.remove(resourceHandle);
        handler.deleteResource(resourceHandle);
    }

    @Override
    public void returnResource(ResourceHandle resourceHandle) {
        if(_logger.isLoggable(Level.FINE)){
            _logger.fine("returnResource called.");
        }
        resourceHandle.setBusy(false);
        freeResourceHandles.offer(resourceHandle);
    }

    @Override
    public int getFreeListSize() {
        return freeResourceHandles.size();
    }

    @Override
    public void removeAll() {
        if(_logger.isLoggable(Level.FINE)){
            _logger.fine("removeAll called.");
        }
        Iterator it = allResourceHandles.iterator();
        while (it.hasNext()) {
            ResourceHandle next = it.next();
            freeResourceHandles.remove(next);
            handler.deleteResource(next);
            it.remove();
        }
    }

    @Override
    public int getResourcesSize() {
        return allResourceHandles.size();
    }

    @Override
    public ArrayList getAllResources() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
GlassFish Moment Correctness (computer science) Throughput (business) Implementation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 6 Things Startups Can Do to Avoid Tech Debt
  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java
  • Evolving Domain-Specific Languages
  • 5 Steps to Strengthen API Security

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo