Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide StatelessSession for Panache #8348

Closed
nimo23 opened this issue Apr 2, 2020 · 11 comments
Closed

Provide StatelessSession for Panache #8348

nimo23 opened this issue Apr 2, 2020 · 11 comments
Labels
area/panache kind/enhancement New feature or request triage/wontfix This will not be worked on

Comments

@nimo23
Copy link
Contributor

nimo23 commented Apr 2, 2020

Description
Actually, Panache retrieves its entities always with managed entity sessions.

Would be good if Panache has the possibility to query entities with StatelessSession because loading lots of data or doing bulk operations with StatelessSession is better in terms of performance and memory consumption in compare to using no stateless session.

See

Implementation ideas
Have something like PanacheEntity.find(query, params).stateless().list().

@nimo23 nimo23 added the kind/enhancement New feature or request label Apr 2, 2020
@quarkusbot
Copy link

/cc @FroMage @loicmathieu

@geoand
Copy link
Contributor

geoand commented Apr 2, 2020

@maxandersen will also be interested in this :)

@emmanuelbernard
Copy link
Member

I would like to get a clear set of use case where we would recommend people to use this alternative model. I am not sure detached entities plays that nicely with the Panache pattern. Sanne and I pulled our hair on a couple of related cases.
Or are we talking about a panache model that is 100% separated and independent from the existing Hibernate ORM with Panache one?

So can we literally write the documentation that explains when to use which here and right now. Once we have that we can decide to push further.

@nimo23
Copy link
Contributor Author

nimo23 commented Apr 2, 2020

Or are we talking about a panache model that is 100% separated and independent from the existing Hibernate ORM with Panache one?

Good question..a StatelessSession (https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/StatelessSession.html) would be a good alternative of mybatis or jooq when it comes to performance..having the convenience of JPA/Hibernate with the benefits of the performane of myBatis, jooq,..I would like to see to have the option to opt out from the statefullness of hibernate.

JPA also offers view query (queries with Select New):

UserViewObject viewObject = 
em.createQuery("SELECT NEW org.User(c.id, c.name, c.,...) FROM User AS c WHERE...");

Queries with Select New are

  • not managed by the container
  • is much faster
  • and consumes less memory

But in compare to view queries the org.hibernate.StatelessSession can also do CRUD-bulk operations.

@maxandersen
Copy link
Member

I think the question is wether its worth it for Panache to integrate with StatelessSession as opposed to just use stateless session directly ?

@nimo23
Copy link
Contributor Author

nimo23 commented Apr 3, 2020

Few examples to show the boilerplate code needed when using StatelessSession directly in compare to Panaches stateless CRUDs:

Read operation:

StatelessSession statelessSession = sessionFactory.openStatelessSession();
  try {
    statelessSession.getTransaction().begin();
    Query query = statelessSession
        .createQuery(
            " SELECT u.id, u.firstName, u.lastName FROM User u WHERE mood = 'Good'")
        .setFetchSize(0).setReadOnly(true);
    ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY);
    while (scroll.next()) {
      log.info("Entity: " + scroll.get(0));
    }
    statelessSession.getTransaction().commit();
  } catch (Exception ex) {
    statelessSession.getTransaction().rollback();
    throw ex;
  } finally {
    statelessSession.close();
  }

instead of

PanacheEntity.find(query, params).stateless().list()

Bulk delete operation:

// code around try..catch..
final StatelessSession session = persister.getFactory().openStatelessSession();
Transaction tx = session.beginTransaction();
var query = session.createQuery("DELETE FROM User WHERE date = :date");
query.setParameter("date", key);
query.executeUpdate();
tx.commit();
session.close();

instead of

PanacheEntity.delete(query, params).stateless();

Bulk insert operation:

// code around try..catch..
StatelessSession session = sessionFactory.openStatelessSession();
Transaction transaction = session.beginTransaction();
for (int i = 0; i < 1000000; i++) {
    User u = new User();
    u.setVal(i);
    session.insert(u);
}
transaction.commit();

instead of

PanacheEntity.insert(listOfNewUsers).stateless();

@FroMage
Copy link
Member

FroMage commented Apr 3, 2020

This looks to me like you want a substitute of not only the Panache API but also the transactional interceptor and the CDI bit that allocates a new EntityManager to each request, so that it would allocate a stateless session instead.

@loicmathieu
Copy link
Contributor

I agree with @FroMage this has nothing to do with Panache but directly to Hibernate ORM support. I would re-target this issue to Hibernate ORM extension.

To implements this, we will need something close to what we try to do for readonly transaction (that are read only Hibernate sessions) in PR #7455

By the way, is a stateless session different from a read only session performance wise ?
What will happen if you mix stateless and statefull session in the same transaction ?

@nimo23
Copy link
Contributor Author

nimo23 commented Apr 3, 2020

This looks to me like you want a substitute of not only the Panache API..

I wanted to have a layer over Hibernate/JPA (e.g., Panache API) which gives up some ORM overhead with the aim to have a (near) equal competitor (in regards to speed and memory consumption) for iBatis, jooq, This layer only uses on-board means of Hibernate/JPA/Quarkus (e.g., Stateless Session,..). Having such a layer could make the integration of iBatis, mybatis, jooq, etc. needless..

By the way, is a stateless session different from a read only session performance wise ? What will happen if you mix stateless and statefull session in the same transaction ?

Also good questions, I don't know:) maybe @Sanne can answer it..

@maxandersen
Copy link
Member

By the way, is a stateless session different from a read only session performance wise ?
What will happen if you mix stateless and statefull session in the same transaction ?

same as if you had two statelesssessions or two stateful session on same transaction - they would work in same transactional boundary but you risk some state in your objects to be out of sync.

But that is not necessarily a bad thing; but my recommendatin would though to generally don't mix and match blindly ;)

@nimo23
Copy link
Contributor Author

nimo23 commented Apr 4, 2020

By the way, is a stateless session different from a read only session performance wise ?

If StatelessSession is faster for read operation then it could be an alternative for #7455.

I think the question is wether its worth it for Panache to integrate with StatelessSession as opposed to just use stateless session directly ?

Now, I also think it is not worth to include a full support of StatelessSession patterns within Panache. The StatelessSession can be used directly and this should be sufficient. So I will close this issue.

I agree with @FroMage this has nothing to do with Panache but directly to Hibernate ORM support. I would re-target this issue to Hibernate ORM extension. To implements this, we will need something close to what we try to do for readonly transaction (that are read only Hibernate sessions) in PR #7455

If someone thinks it is worth to implement then feel free to open it again.

@nimo23 nimo23 closed this as completed Apr 4, 2020
@gsmet gsmet added the triage/wontfix This will not be worked on label Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/panache kind/enhancement New feature or request triage/wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

8 participants