Fixing issue with Classloader leaks.

We had an issue with a classloader leak. There were three things we had to do before it worked.

  1. Made the mysql connector provided and add mysql-connector jar to the tomcat/libexe/lib directory
    1. Maven block:
      <scope>provided test</scope>
  2. Added the following jvm option to use G1 garbage collection, and enable class unloading and perm gen sweep.
    NOTE: You need all three for this to work. Ignore the warning from java: “Please use CMSClassUnloadingEnabled in place of CMSPermGenSweepingEnabled in the future.” I’ve tried just G1, G1 with class unload, and G1 with perm gen. They are all needed for heap to stay low and for classes to be unloaded.

    -XX:+UseG1GC -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled
  3. Added the following library for classloader leak prevention.
    1. Add the following block in your pom.xml of the web module:
    2. Add the following listener to your web.xml:

Once that is all setup, do a clean package on maven, redeploy and restart tomcat


Jpa 2.0 performing a count on a query with joins

It’s been too long since I’ve blogged. Here is an interesting one. I recently had to modify old code. They were using jpa 2.0 in more of a 1.0 syntax. There was a very complex query with joins used for searching. There was pagination, sort of. They were not returning the total record count. So I decided to do a count on the same query as a happy medium until we can upgrade to a newer way of doing things.

I tried using the same query and predicates to do the count. I tried following several examples from stack overflows and forums. But, all the examples were with simple queries that did not do joins.

The biggest thing here is that I had to use different query objects and different predicate objects, while using the same root when creating the count predicates and the count query. It appears that query.from() creates a different root each time it’s called. So, I set this to a variable and reused it.

I’ve left out the complex code that generates the predicates to save space. I can add it if needed. It is in the method getPredicates(searchCustomerRequest, criteriaBuilder, customerRoot); which is also used by searchCustomer(searchCustomerRequest); to build the same predicate list.

Map results = new HashMap();
CriteriaBuilder criteriaBuilder = getRoEntityManager().getCriteriaBuilder();
CriteriaQuery countQuery = criteriaBuilder.createQuery(Long.class);

Root customerRoot = countQuery.from(Customer.class);
List lstCriteria = getPredicates(searchCustomerRequest, criteriaBuilder, customerRoot);
Predicate[] predicates = lstCriteria.toArray(new Predicate[lstCriteria.size()]);

/*Start count*/
Logger.debug("Getting Count");;
Long totalRecordCount = getRoEntityManager().createQuery(countQuery).getSingleResult();

List lstCustomer = searchCustomer(searchCustomerRequest);

results.put("customerList", lstCustomer);
results.put("totalRecordCount", totalRecordCount);
return results;

Teamcity Incompatible Runner

While using teamcity, we run into the issue where an agent changes and becomes incompatible with certain projects.  The message is “Incompatible Runner: Ant”.  We are using Maven.  I am not 100% sure that this is do to ownership.  But, when I see this message, I will login to the build server and run the following command:

chown -R appbuilder:ApprissUsers /opt/appriss/buildAgents

Then I restart the buildagents and everything is back to normal.  I believe that this error occurs because someone is restarting the agents without using the appbuilder user, like I mention in my other post about agents being down.

Exclude URL from EhCache Web Cache

You would think that this would be built-in, to easily exclude a URL. Maybe there is a good reason they left it out. Anyway, I recently needed to cache results from webservice hits, so that we could minimize the load on the database. I liked the idea of caching the response to really minimize the execution of code. EhCache Web Cache seemed perfect, until I had to exclude one specific service from the cache. The specs were already given to the customer and the URL could not change. So, I chose to use filters to prevent caching.

First, I created a filter and overrode the doFilter method as seen below.

public class NoCacheFilter implements javax.servlet.Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
        /** do nothing **/

    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        if (request instanceof HttpServletRequest
            && response instanceof HttpServletResponse) {

            HttpServletRequest httpRequest = (HttpServletRequest) request;
            String requestUri = httpRequest.getRequestURI();
            if(requestUri.contains("/registration/save")) {
                requestUri = requestUri.replace("/vine/", "/vine-nocache/");
                       .forward(request, response);
            } else {
                chain.doFilter(request, response);

    public void destroy() {
        /** do nothing **/

Second, I created another controller for URLs that should not be cached.

public class VineNoCacheController {

    private WebServiceTemplate vineRegisterWsTemplate;

    @RequestMapping(value = "/site/{siteId}/agency/{agencyId}/offender/{subjectId}
                    produces = "application/json")
    public void registerVictim(@PathVariable Integer siteId,
                               @PathVariable Integer agencyId,
                               @PathVariable String subjectId,
                               @RequestParam(required = false) Boolean enableTTY,
                               @RequestParam(required = false) String pin,
                               @RequestParam EnumLanguage language,
                               @RequestParam String uniqueDeviceId,
                               HttpServletResponse response) throws IOException {

// code omitted to save space

Third, I configured the filter in the web.xml. The filters execute in order. So, by putting the no cache filter first, it will execute that doFilter method and if the URI contains the “registration/save”, then forward the request to vine-nocache. If it does not contain that String, then continue down the filter chain(which the next filter is the web cache filter). By using a forward, the end user never knows that it is a different URL.


If there is a better way, please share. But, this is the best way I found in a short period of time.

TeamCity Build Agent Disconnected

If an agent becomes disconnected for whatever reason, you can follow these steps to re-connect it:

ssh to the server that the disconnected agent is on.

go to the build agent’s bin directory (i.e. /opt/teamcity/buildagent/buildagent01/bin/)

su to the user that the agent runs as (i.e. appBuilder)

run start

That will start the agent, and after a few seconds or so, the next time you go to TeamCity, it will be connected.