java.lang.IllegalStateException: Session/EntityManager is closed

I have a rest api service that implements RealmResourceProvider, RealmResourceProviderFactory interface. It works correctly without a problem. However, I also need to access to the h2 database. Then when I add EntityManager as the class member, accessing through curl command, following error is thrown.

Caused by: java.lang.IllegalStateException: Session/EntityManager is closed
	at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:475)
	at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:187)
	at org.hibernate.internal.SessionImpl.find(SessionImpl.java:2415)
	at org.hibernate.internal.SessionImpl.find(SessionImpl.java:2400)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.keycloak.connections.jpa.PersistenceExceptionConverter.invoke(PersistenceExceptionConverter.java:66)
	... 13 more

The way I use EntityManager is

@Path("/realms/{realm}/")
public class MyRest {
      @PersistenceContext(unitName = "keycloak-default")
      EntityManager em;
      public MyRest() {
          this.em = session.getProvider(JpaConnectionProvider.class).getEntityManager();
      }

      @Path("service/myendpoint")
      @POST
      @Consumes({MediaType.APPLICATION_JSON})
      @Produces({MediaType.APPLICATION_JSON})
      public Response myendpoint(final MyData data) {
          String id = UUID.randomUUID().toString();
          UserEntity user = this.em.find(UserEntity.class, id); 
          // some operation set user entity data
         this.em.persistent(user);
         user = this.em.find(UserEntity.class, id); // the operation fails because entity manager is closed
          ...
      }
}

I read this link. It recommends not placing eneitymanager in provider. And I consults the jpa example. I attempted to follow the example, but MyRestProvider class can not implement UserStorageProvider, because it has already implemented RelmResourceProvider.

How should I use EntityManager to prevent this EntityManager is closed problem? Thanks

Ok. Switching to this example fixes my problem. The example contains the usage with the combination of RESTful APIs, JPA throuh SPI. Thanks