How can I prevent from a close method being invoked?

Jin Kwon

I'm planning to write a business method for those non-container-managed clients such as JRX-RS resources.

@Stateless
public class PersistenceService {

    public <R> R apply(final Function<EntityManager, R> function) {
        return function.apply(entityManager);
    }

    @PersistenceContext
    private transient EntityManager entityManager;
}

So that a JAX-RS root resource class uses it like this.

@RequestScoped
@Path("/entities")
public class MyEntites {

    @GET
    @Path("/{id: \\d+}")
    public Response read(final long id) {

        return Response.ok(service.apply(m -> {
            m.find(MyEntity.class, id);
        })).build();
    }

    @Inject
    private transient PersistenceService service;
}

The problem is that how can I prevent clients invoking close() on specified entity manager?

service.apply(m -> {
    m.close();
    return null;
});
Tobias Liefke

You can create a wrapper around the original entity manager, which ensures that close() is never called:

public class NoCloseEntityManager implements EntityManager {
  private EntityManager wrapped;
  public NoCloseEntityManager(EntityManager wrapped) {
    this.wrapped = wrapped;
  }

  public void close() { 
    throw new UnsupportedOperationException("Don't call close()");
  }

  // all other methods from the interface call the wrapped manager:
  public void persist(Object entity) {
    wrapped.persist(entity);
  }
  ...
}

That one can you use in your service:

@Stateless
public class PersistenceService {

    @PersistenceContext
    private transient EntityManager entityManager;

    public <R> R apply(final Function<EntityManager, R> function) {
        return function.apply(new NoCloseEntityManager(entityManager));
    }

}

But I don't know if the effort is worse the result, as entityManager.close() isn't called very often - if someone calls it, he will not use it by mistake...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can a close() method invoked from Stream point to the implementation of the close() method in the AbstractPipeline abstract class?

From Dev

How to prevent a method returning a Future from being invoked until a previous call does not complete

From Dev

Why is the keyup handler being invoked here, and can I prevent it?

From Dev

How can I prevent a sinon spy from calling the method being spied on?

From Dev

How can I prevent my parameter in my setter method from being changed?

From Dev

How can I prevent methods from being added to a class?

From Dev

How can I prevent a Dockerfile instruction from being cached?

From Dev

How can I prevent a window from being resized with tkinter?

From Dev

How can I prevent the action bar from being cut off?

From Dev

How can I prevent this script from being freely accessed?

From Dev

How can I prevent a column from being dragged out of the grid?

From Dev

How can I prevent PDF files from being opened?

From Dev

How can I prevent iwconfig power management from being turned on?

From Dev

How can I prevent a certificate from being trusted?

From Dev

How can I prevent a folder from being inadvertently deleted by myself?

From Dev

How can I prevent this script from being freely accessed?

From Dev

How can I prevent arguments to `xargs` from being prefixed with spaces?

From Dev

How can I prevent certain process from being swapped?

From Dev

How do I prevent just 'exit' from being saved when I close an SSH connection?

From Dev

Can I prevent an interface from being implemented?

From Dev

When running a method from a Python superclass, how can I know the name of the child class that invoked it?

From Dev

When running a method from a Python superclass, how can I know the name of the child class that invoked it?

From Dev

doGet() Servlet method not being invoked from JSP

From Dev

How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

From Dev

How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

From Dev

In Android, How can I avoid the onStart method from being deprecated?

From Dev

In Android, How can I avoid the onStart method from being deprecated?

From Dev

how to get reference to the Method being invoked in jersey

From Dev

Play: How to prevent the body parser from being invoked in case the action code does not get executed

Related Related

  1. 1

    How can a close() method invoked from Stream point to the implementation of the close() method in the AbstractPipeline abstract class?

  2. 2

    How to prevent a method returning a Future from being invoked until a previous call does not complete

  3. 3

    Why is the keyup handler being invoked here, and can I prevent it?

  4. 4

    How can I prevent a sinon spy from calling the method being spied on?

  5. 5

    How can I prevent my parameter in my setter method from being changed?

  6. 6

    How can I prevent methods from being added to a class?

  7. 7

    How can I prevent a Dockerfile instruction from being cached?

  8. 8

    How can I prevent a window from being resized with tkinter?

  9. 9

    How can I prevent the action bar from being cut off?

  10. 10

    How can I prevent this script from being freely accessed?

  11. 11

    How can I prevent a column from being dragged out of the grid?

  12. 12

    How can I prevent PDF files from being opened?

  13. 13

    How can I prevent iwconfig power management from being turned on?

  14. 14

    How can I prevent a certificate from being trusted?

  15. 15

    How can I prevent a folder from being inadvertently deleted by myself?

  16. 16

    How can I prevent this script from being freely accessed?

  17. 17

    How can I prevent arguments to `xargs` from being prefixed with spaces?

  18. 18

    How can I prevent certain process from being swapped?

  19. 19

    How do I prevent just 'exit' from being saved when I close an SSH connection?

  20. 20

    Can I prevent an interface from being implemented?

  21. 21

    When running a method from a Python superclass, how can I know the name of the child class that invoked it?

  22. 22

    When running a method from a Python superclass, how can I know the name of the child class that invoked it?

  23. 23

    doGet() Servlet method not being invoked from JSP

  24. 24

    How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

  25. 25

    How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

  26. 26

    In Android, How can I avoid the onStart method from being deprecated?

  27. 27

    In Android, How can I avoid the onStart method from being deprecated?

  28. 28

    how to get reference to the Method being invoked in jersey

  29. 29

    Play: How to prevent the body parser from being invoked in case the action code does not get executed

HotTag

Archive