The best way to initialize LAZY entity and collection proxies with JPA and Hibernate

Introduction

In this article, we are going to see the best way to initialize LAZY proxies and collections when using JPA and Hibernate.

I decided to write this article because there are way too many resources available on the Internet that mislead the reader into using awkward and inefficient practices.

Continue reading “The best way to initialize LAZY entity and collection proxies with JPA and Hibernate”

How do find and getReference EntityManager methods work when using JPA and Hibernate

Introduction

While doing my High-Performance Java Persistence training, I realized that not all developers are familiar with the getReference method of the JPA EntityManager and most of them use find almost exclusively.

In this article, we are going to see the difference between the find and getReference method so that it’s clear when to apply them depending on the underlying use case.

Continue reading “How do find and getReference EntityManager methods work when using JPA and Hibernate”

How does a JPA Proxy work and how to unproxy it with Hibernate

Introduction

The JPA lazy loading mechanism can either be implemented using Proxies or Bytecode Enhancement so that calls to lazy associations can be intercepted and relationships initialized prior to returning the result back to the caller.

Initially, in JPA 1.0, it was assumed that Proxies should not be a mandatory requirement, and that’s why @ManyToOne and @OneToOne associations use an EAGER loading strategy by default. However, EAGER fetching is bad for performance so it’s better to use the FetchType.LAZY fetching strategy for all association types.

In this article, we are going to see how the proxy mechanism works and how you can unproxy a given Proxy to the actual entity.

Continue reading “How does a JPA Proxy work and how to unproxy it with Hibernate”

How does FlexyPool support both Connection proxies and decorators

Proxies

FlexyPool monitors connection pool usage and so it needs to intercept the connection close method call.
For simplicity sake, the first version was relying on dynamic proxies for this purpose:

private static class ConnectionInvocationHandler 
    implements InvocationHandler {

    public static final String CLOSE_METHOD_NAME = "close";

    private final Connection target;
    private final ConnectionCallback callback;

    public ConnectionInvocationHandler(
        Connection target, 
        ConnectionCallback callback) {
        this.target = target;
        this.callback = callback;
    }

    @Override
    public Object invoke(
        Object proxy, 
        Method method, 
        Object[] args) throws Throwable {
        if (CLOSE_METHOD_NAME.equals(method.getName())) {
            callback.close();
        }
        return method.invoke(target, args);
    }
}

As straightforward as it may be, a proxy invocation is slower than a decorator, which calls the target method using a direct invocation.
Because all connection pools use proxies anyway, adding another proxy layer only adds more call-time overhead and so now FlexyPool supports connection decorators as well.

Continue reading “How does FlexyPool support both Connection proxies and decorators”