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

easy access to the JDBC connection associated with the EM #432

Closed
gavinking opened this issue Aug 8, 2023 · 6 comments · Fixed by #433
Closed

easy access to the JDBC connection associated with the EM #432

gavinking opened this issue Aug 8, 2023 · 6 comments · Fixed by #433

Comments

@gavinking
Copy link
Contributor

gavinking commented Aug 8, 2023

JPA currently provides no easy way to just run some JDBC code within the transaction/connection associated with the EntityManager. This isn't such a big deal when using JPA inside a container, but it's a pretty big limitation when using it in standalone mode.

Hibernate has an API like this, as part of our Session:

	/**
	 * Perform work using the {@link java.sql.Connection} underlying by this session.
	 *
	 * @param work The work to be performed.
	 *
	 * @throws HibernateException Generally indicates wrapped {@link java.sql.SQLException}
	 */
	void doWork(Work work) throws HibernateException;

	/**
	 * Perform work using the {@link java.sql.Connection} underlying by this session,
	 * and return a result.
	 *
	 * @param work The work to be performed.
	 * @param <T> The type of the result returned from the work
	 *
	 * @return the result of calling {@link ReturningWork#execute}.
	 *
	 * @throws HibernateException Generally indicates wrapped {@link java.sql.SQLException}
	 */
	<T> T doReturningWork(ReturningWork<T> work) throws HibernateException;

I propose we add something very similar, or even almost identical to EntityManager.

@gavinking
Copy link
Contributor Author

Please see draft proposal in #433

gavinking added a commit to gavinking/persistence that referenced this issue Aug 10, 2023
@gavinking gavinking linked a pull request Aug 11, 2023 that will close this issue
@hantsy
Copy link
Contributor

hantsy commented Aug 13, 2023

I would like unite the naming to use withConnection or doWithConnection directly, the run/call will lead developers to think it will accept a Runnable or Callable as parameter at the first sight.

@gavinking
Copy link
Contributor Author

Well, actually I quite deliberately chose "run" and "call" to make you think of the difference between Runnable and Callable. The difference between these two operations is very much analogous.

I'm not very keen on your suggested convention because:

  1. the names of the two operations are asymmetric, and
  2. "with" vs "doWith" isn't suggestive at all: I suppose you mean to stick "do" on the one which doesn't return a value, but even that's not 100% obvious.

Do you have a different suggestion?

@gavinking
Copy link
Contributor Author

Note that "executeWith" and "computeWith" work and are very clear, but they're also quite verbose. (I could live with them if that's what others prefer.)

@hantsy
Copy link
Contributor

hantsy commented Aug 20, 2023

I just see some examples like these, eg. Micronaut Data use withXXX in the XXXOperations or TransactionsOperator and Hibernate Reactive SessionFactory itself contains withSession/withTransaction, https://github.com/hantsy/quarkus-sandbox/blob/master/resteasy-reactive-hibernate/src/main/java/com/example/PostRepository.java.

@gavinking
Copy link
Contributor Author

@hantsy The reason that works out nicely in reactive is that every operation returns a Uni, so one single method definition can handle the case of Uni<Result> and Uni<Void>. But here we need the overload.

gavinking added a commit to gavinking/persistence that referenced this issue Aug 21, 2023
lukasj added a commit that referenced this issue Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants