- 浏览: 544998 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (251)
- UML/RUP/软件工程 (0)
- DDD:领域驱动设计 (1)
- IaaS/Paas/SaaS (1)
- Hadoop/YARN (10)
- PBDP项目 (2)
- OSGi-基础 (7)
- OSGi-Aries (2)
- OSGi-SpringDM (32)
- Oracle/MySQL/SS (8)
- Felix/Equinox/Bnd (14)
- Virgo Server/Gemini (7)
- Weblogic/JBoss/Tomcat (10)
- ActiveMQ (14)
- Camel (1)
- Spring Roo/Eclipse (7)
- Java/JSP/JSF (10)
- Maven (19)
- ESB-Mule (1)
- ESB-ServiceMix (18)
- SOA/SCA/SDO (12)
- WebService/RESTful (17)
- JS/jQuery/ExtJS (4)
- Spring/JPA/MVC (15)
- SpringSecurity (5)
- BPM/jBPM (3)
- Hudson/SVN/CI (0)
- LinuxOS/虚拟化 (25)
- Office/OpenOffice (1)
- 项目管理 (5)
- POI/jFreeChart (1)
最新评论
-
panamera:
请问JMS Transport 发布的webservice 是 ...
CXF 提供的Service Transport-JMS Transpor -
jianyi296:
where is attachment.
CXF WebService Dynamic Client -
hj01kkk:
您好,我用jdbc-user-service为什么每次验证时都 ...
SpringSecurity笔记3-Authenticating Users:Authenticaton Strategy -
wufenglin1231:
你好,我在实现Mapping exceptions to re ...
RESTful 异常处理 -
xchd:
[echo] Project: common
[echo ...
Hive安装配置学习笔记
1. Spring’s transactional support
Spring’s transactional support consists of two main parts for managing transactions on beans that
are managed by the container:
■ Transactional synchronization
—This manages resources used to handle transactions. This
mechanism is directly integrated into Spring’s data access support and is based on a transactional
context stored in a variable of type ThreadLocal.
■ Transaction demarcation
—This allows you to apply transactions to application logic. Although an
API is available, Spring also provides features to declaratively apply them to Spring-managed beans
through AOP and annotations.
The key abstraction for this support is the PlatformTransactionManager interface, which provides a
contract for transaction demarcation. The following snippet shows the content of this interface:
public interface PlatformTransactionManager {
TransactionStatus getTransaction(TransactionDefinition definition);
void commit(TransactionStatus status);
void rollback(TransactionStatus status);
}
In the PlatformTransactionManager interface, the getTransaction method allows you to initialize
and start a transaction. The commit method successfully finalizes a transaction; the rollback
one cancels it.
Implementations of this interface are provided for each data access implementation in Spring.
These implementations are commonly based on factories for the corresponding technology
(because they must be able to access resources to manage transactions), or, in the case of JTA,
on the Java transaction manager.
With respect to transaction propagation, Spring defaults to transactions being REQUIRED on
beans, which allows application components to participate in the current transaction or to
create it if it doesn’t exist. In addition, Spring introduces read-only transactions, which can help
improve performance in ORM tools.
You can configure transactions in Spring by following these steps:
■ Create and configure all your POJOs in the Spring container.
They will correspond to abstractions
for data access, DAO entities, and business services.
■ Configure a transaction manager bean, whose implementation depends on the underlying
persistence technology.
■ Apply transactional behaviors to business services using either XML or transaction annotations.
Transaction configuration using AOP and XML:
<bean id="transactionManager" (...)>
(...)
</bean>
<aop:config>
<aop:advisor
pointcut="execution(* *..*ServiceImpl.*(..))"
advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
@Transactional
public interface ContactsService {
@Transactional(readOnly=true)
List<Contact> getContacts();
void addContact(Contact contact);
}
Another possibility is to use the Transactional annotation to specify transactional behavior
directly in interfaces and classes. Information specified on interfaces is automatically
used in corresponding implementations and can be overridden if necessary. The annotation
can be specified both at the class level or method level. At the class level, it provides global
configuration for all methods, which can then be overridden if another configuration is required.
Transactional annotation support isn’t activated by default and must be enabled through the
annotation-driven element of Spring’s tx namespace, as shown in the following snippet:
<bean id="transactionManager" (...)>
(...)
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
2. Using JPA transactions with Spring DM
As emphasized in the previous section, the central abstraction of Spring’s transactional
support is the PlatformTransactionManager.
When using local transactions,
the transaction
manager implementation depends on the underlying data access technology, whereas
for global transactions Spring provides a specific implementation that interacts with the
underlying JTA transaction manager (which is usually hosted by an application server).
We’ll describe here only the local transaction approach and use the annotation-driven
method of configuring transactions.
We can identify two use cases for configuring transaction management, depending on the
organization of bundles within an application:
■ Services and DAOs are in the same component. In this case, Spring’s transaction support can
be used as in regular Spring applications. The EntityManagerEntity entity, services, DAO, and
Spring transaction manager are all located in the same Spring container.
■ Services and DAOs are in different components.
In this case, services, the EntityManagerEntity
entity, and the Spring transaction manager are located in different components. Because you
need a transaction manager to apply transactions to services, this must be registered as an
OSGi service. The service component is then able to reference and use it.
In the first use case, bundles are autonomous(自治的), containing both services and DAO entities,
and they embed their own transaction managers. Transactions are managed within these
components, and transactions are already applied to entities registered as OSGi services.
In the use case where services and DAOs are separated, the Spring transaction manager can be
provided as an OSGi service to a service component by the DAO component. In the service
component, this service is used to apply transactions to services.
The service for the Spring transaction manager is registered under the
PlatformTransactionManager interface, which is the root interface for all transaction managers
within Spring. That makes this service independent from the underlying data access technology.
<bean id="entityManagerFactory" class="(...)">
(...)
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<osgi:service ref="transactionManager"
interface="org.springframework.transaction.PlatformTransactionManager"/>
<osgi:reference id="transactionManager"
interface="org.springframework.transaction.PlatformTransactionManager"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
Import-Package: (...)
javax.persistence.spi;version="1.0.0",
org.aopalliance.aop;version="1.0",
org.springframework.aop;version="3.0.0.M1",
org.springframework.aop.framework;version="3.0.0.M1",
org.springframework.transaction.annotation;version="3.0.0.M1 "
We saw that Spring provides the Transactional annotation for describing transactional behavior. A
good practice consists of using this annotation on service interfaces. All the implementation classes
will then directly benefit from the definition of transactional behavior at the interface level.
发表评论
-
SpringDM笔记31-Testing with OSGi and SpringDM
2011-11-22 10:27 12461. 创建一个SpringDM测试类 SpringD ... -
SpringDM笔记30-OSGi中使用SSL/STL
2011-11-21 11:55 1319SSL:Secure Sockets Layer ... -
SpringDM笔记29-Require-Bundle与Import-Package的区别
2011-11-21 10:31 2238具体可参考:http://www.osgi.org/bl ... -
SpringDM笔记28-OSGi Bundle Activities with Spring-DM
2011-11-17 10:19 1159OSGi框架中也支持搞层次的模块交互:bundles.例如 ... -
SpringDM笔记28-Spring And OSGi:Layers of Integration
2011-11-15 11:00 11761. Application Design:Service和B ... -
SpringDM笔记27-Extending The Stand Extender and Configure
2011-09-02 09:38 8691. -
SpringDM笔记25-Using AJAX frameworks with Spring DM:GWT
2011-09-01 08:53 12011. Using Spring DM with AJAX fr ... -
SpringDM笔记24-Using action-based web frameworks with Spring DM:SpringMVC
2011-08-30 09:33 1478■ Action-based web frameworks ... -
SpringDM笔记23-Using the open EntityManager in view pattern实现延迟加载
2011-08-30 09:27 15671. The open EntityManager in vi ... -
SpringDM笔记21-Using ORM within OSGi with Spring DM
2011-08-25 10:31 2096Version 1.Object/relational ma ... -
SpringDM笔记20-Using JDBC within OSGi with Spring DM
2011-08-25 09:08 1776The public API for interact ... -
SpringDM笔记19-SpringDM如何处理OSGi应用的动态行为
2011-08-24 08:51 1143ServiceTracker 1. Dealing ... -
SpringDM笔记18-Designing OSGi Enterprise Applications
2011-08-22 11:08 11221. Organizing OSGi components ... -
SpringDM笔记17-Handling Collections of OSGi Services
2011-08-20 09:12 15421.Configuring collections:the l ... -
SpringDM笔记16-处理OSGi服务的动态性:事件
2011-08-19 09:51 17801. Service registration and unr ... -
SpringDM笔记15-通过声明特定的属性注册和引用服务
2011-08-18 11:01 14081. Configuration for registerin ... -
SpringDM笔记14-The thread context classloader 及在OSGi中的运用
2011-08-18 10:40 22481. Using the thread context cla ... -
SpringDM笔记13-OSGi服务注册与引用
2011-08-18 09:28 34501. Combining OSGi services and ... -
SpringDM笔记12-Spring DM’s web Extender运行机制
2011-08-17 11:04 2088SpringDM把一个WAR作为一个Bundle, 其实 ... -
SpringDM笔记11-Hard dependencies on the OSGi environment
2011-08-17 10:50 1414Any bean in a Spring DM app ...
相关推荐
IEEE-Transactions-Word-templates-and-instructions.zip
ieee模版 用于发表ieee transaction论文的通用latex模版
IEEE-Transactions期刊的论文格式要求和模板,我从IEEE官网上找到的。
标签:atomikos-transactions-jdbc.jar.zip,atomikos,transactions,jdbc,jar.zip包下载,依赖包
讲解java事务设计策略,对于java中事务的讲解非常的清晰。
标签:atomikos-transactions-jms.jar.zip,atomikos,transactions,jms,jar.zip包下载,依赖包
标签:atomikos-transactions-jta.jar.zip,atomikos,transactions,jta,jar.zip包下载,依赖包
django-celery-transactions django-celery-transactions保留Celery任务,直到提交了当前数据库事务为止,避免了Celery 所述的潜在竞争情况。 从信号处理程序发送任务,不用担心!特征如果回滚事务,则将丢弃任务。 ...
IEEE-Transactions-Word模板
IEEE-Transactions-LaTeX2e-templates-and-instructions模板
#sqlite3-transactions 向添加事务支持。背景Node-sqlite3 是一种访问 SQLite 数据库的好方法,但 id 尚不提供对事务的支持。 底层 SQLite 数据库可以轻松处理事务,因此您仍然可以执行以下操作: db . serialize ( ...
customer-transactions.csv用户转化数据
标签:atomikos-transactions-api.jar.zip,atomikos,transactions,api,jar.zip包下载,依赖包
标签:atomikos-transactions-hibernate3.jar.zip,atomikos,transactions,hibernate3,jar.zip包下载,依赖包
官方版本,亲测可用
官方版本,亲测可用
sails-mysql-transactions, 带有mySQL事务支持的帆/水线 ORM 带有复制支持的MySQL事务性 ORMsails-mysql-transaction 是用于MySQL的船帆ORM适配器,具有事务和复制群集支持。这个适配器在流行的sails-mysql 适配器...
sql2o-spring-transactions-demo 使用 sql2o fork 设置 Spring 事务示例: :
官方版本,亲测可用