JPA : applicationManaged EntityManager for Java SE to control transaction lifecycle programmatically

核能气质少年 提交于 2019-12-05 02:43:43

问题


I'm having difficulties finding a good solution to support this feature where the UI can start and commit the transaction.

In my previous approaches in working with transactional applications, i group the unit of work into a service method in the backend, and annotate it with spring's @Transactional.


But imagine that i have several service methods like this, and it's up to the frontend to group the service method calls within a transaction.

For example, i have methodServiceA, methodServiceB, methodServiceC. The UI could do something like this with any combinations like :

Combination 1 :

  1. starts the transaction
  2. call methodServiceA + call methodServiceB
  3. commits the transaction

Combination 2 :

  1. starts the transaction
  2. call methodServiceB + call methodServiceC
  3. commits the transaction

Combination 3 :

  1. starts the transaction
  2. call methodServiceA + call methodServiceB + call methodServiceC
  3. commits the transaction

Basically, the backend provides only the services method, and it's up to the UI or other applications that make use of the backend to start / commit the transaction.


So that is basically the situation i'm dealing with .. and here's the thing i have in mind. Please share some other options or perhaps improvements i could make to support this feature. Im currently thinking of using the application managed entitymanager, since i dont think using the @Transactional would work in this case.

Im thinking of an object that the UI or other connectors could use to :

  1. create an entity manager and associates it with a unique id
  2. start the transaction from the em
  3. set the timeout from the em
  4. commit the transaction from the em
  5. automatically rollback the transaction if there're any exceptions from the em
  6. feeds the transaction's entity manager to the service methods, so that they use the same entity manager
  7. finally closes the entity manager after commit or rollback from the em

So, for the example for Combination 1, the flow is something like this :

  1. ui starts the transaction using the tool, and get the entitymanager id
  2. pass the entitymanager id while calling methodServiceA + calling methodServiceB, so those methods could use the correct entity manager which is associated with the id
  3. commits the transaction

Please share your ideas on this matter, Thank you !


Concerning the facade / command pattern :

Thanks for the idea. But i've thought about this also, and i dont think it's suitable for our needs since i cant always provide the facade service in the backend for every needs(imagine every ui buttons that can combine any method services that they want) that arise.

The basic idea is to have public service methods that other frontend applications could wire together.

And also, using the facade pattern means no ui logic in the facade method. In our case, the ui logic can be done along with the transaction handling and calling the service methods in the frontend.


回答1:


Use the facade pattern : create a facade service containing a method for each of the combinations (which should map t a functional use-case), and make these facade methods transactional using the Spring annotation. The facade methods will just call the existing service methods.




回答2:


You can try to use a Command pattern - UI sends a sequence of commands, and your backend executes them in a single transaction.

Alternatively, take a look at the concept of extended persistence context - it's something similar to your proposed approach, though in that case steps don't share the same transaction. Perhaps some thoughts regarding it would be relevant in your case.



来源:https://stackoverflow.com/questions/5089411/jpa-applicationmanaged-entitymanager-for-java-se-to-control-transaction-lifecy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!