English 中文(简体)
@Transaction methods
原标题:Executing code before and after @Transactional method
  • 时间:2011-07-12 13:01:06
  •  标签:
  • java
  • spring

我们的春季应用是一个服务层,附加说明的是@Transactional。 我们需要制定一些交易方法前后的守则,理由如下:

  1. We need to synchronize access to the method based on a key. The thread needs to block before the start of the transaction.
  2. We need to post a message on a queue if the transaction succeeds.

选择似乎是:

  1. Create a class with similar methods to the service that can run the @Transactional method in a synchronized block and check for the return then post the message (would need a separate class due to AOP proxy problem). Services calling services, not nice, feels like a work-around.
  2. Write an aspect to wrap around the @Transactional AOP which can do the synchronization and message posting. Might work but would rather avoid AOP.
  3. Move the transaction down to the domain layer. Not desirable or possibly even feasible with the current implementation due to the way domain methods are reused in different workflows.
  4. Code the transaction by hand in the service method and scrap @Transactional.

我想这是一个相当共同的要求。 也许我没有选择5,这是显而易见的。

问题回答

I think I d go with 2 unless you have some specific reasons to avoid AOP. Your problem is a classic example of where AOP can be used and it looks pretty good in the result. Here is a nice example of how to implement that (if you didn t read that already): Advising transactional operations

否则,“AOP”实际上不是一个选择,那么我就选择了@Lawrence McAlpin提出的选择。

I would use a TransactionTemplate (your option 4) and programatically control the scope of the transaction in situations like this.

否则,你就可以把你方法的逻辑转化为单独的服务,使新的服务......-交易,从现行方法中删除“交易”,然后用你在交易前后的逻辑对新服务的需求进行四舍五入。 我也采取了这一办法,但我更喜欢programmatic trade management,因为我认为它更清洁,而且正如你提到的那样,呼吁服务(这是第一种服务的唯一需要)就象一个黑板的工作。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签