вход по аккаунту


The Spring Framework - Introduction to Lightweight

код для вставкиСкачать
Transaction Management with
the Spring Framework
Jürgen Höller
Transaction Demarcation
Transaction Managers
Data Access
ORM Transactions
Introduction (1)
 Spring is a general application framework
в–« addresses overall application architecture
• internal structure of an application
в–« focus on consistent programming model
• decoupling from concrete runtime environment
в–« supports any kind of Java application
• special support for J2EE environments
 Open source project on SourceForge
▫ founded by Rod Johnson & Jürgen Höller
в–« Apache license
Introduction (2)
 Foundation: core container
в–« Inversion of Control
• general lifecycle management
• for any kind of application components
в–« Dependency Injection
• wiring between application components
• instead service lookups
 Further foundation: AOP framework
в–« proxy-based AOP for POJOs
в–« flexible combination of interceptors
Introduction (3)
public class PetStoreImpl implements PetStoreFacade {
private OrderDao orderDao;
private ItemDao itemDao;
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
public void setItemDao(ItemDao itemDao) {
this.itemDao = itemDao;
public void insertOrder(Order order) {
Introduction (4)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<bean id="itemDao"
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="sqlMap"><ref bean="sqlMap"/></property>
<bean id="petStore"
<property name="orderDao"><ref bean="orderDao"/></property>
<property name="itemDao"><ref bean="itemDao"/></property>
Introduction (5)
 Higher-level functionality on top
в–« transaction management
в–« data access support
в–« lightweight remoting, etc
 Subsystems can be used individually
в–« e.g. just Spring transaction management
в–« programmatically, in a plain library style
 Subsystems integrate nicely as a stack
в–« transaction management within core container
в–« declarative transactions via AOP
Introduction (6)
 Spotlight: transaction management
в–« separates transaction demarcation from the
concrete backend transaction manager
• consistent transaction demarcation style
• seamless support for both native transactions
and J2EE server transactions (JTA)
в–« flexible replacement for EJB CMT
 Relevant for many applications
в–« important part of overall application architecture
в–« in general, any kind of data access should operate
within a transaction
Transaction Demarcation (1)
 Transaction definition
в–« propagation behavior
в–« isolation level
в–« read-only flag
• defaults to false
в–« transaction timeout
• defaults to none
в–« transaction name
• defaults to current method name (in declarative case)
Transaction Demarcation (2)
 Propagation behaviors
в–« supporting all EJB CMT propagation codes
в–« also supports NESTED
• if supported by underlying transaction manager
Transaction Demarcation (3)
 Programmatic demarcation a la JTA
в–« direct PlatformTransactionManager usage
в–« TransactionTemplate with TransactionCallback
 Declarative demarcation for arbitrary POJOs
в–« XML-based proxy definitions
• TransactionProxyFactoryBean definition per target bean
в–« auto-proxy creator mechanism
• centralized XML transaction metadata for multiple beans
• instead of specific transaction proxy definitions
в–« transaction metadata in Java source code
• Jakarta Commons Attributes
• J2SE 5.0 annotations
Transaction Demarcation (4)
 TransactionTemplate usage example
public class PetStoreImpl implements PetStoreFacade {
private PlatformTransactionManager ptm;
public void setTransactionManager(PlatformTransactionManager ptm) {
this.ptm = ptm;
public void insertOrder(Order order) {
TransactionTemplate tt = new TransactionTemplate(this.ptm);
tt.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus ts) {
// perform some transactional operations
return null;
Transaction Demarcation (5)
<bean id="petStoreTarget"
<property name="orderDao"><ref bean="orderDao"/></property>
<property name="itemDao"><ref bean="itemDao"/></property>
<bean id="transactionManager"
<bean id="petStore"
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref bean="petStoreTarget"/></property>
<property name="transactionAttributes">
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
Transaction Demarcation (6)
 Example for J2SE 5.0 annotations
public class PetStoreImpl implements PetStoreFacade {
private OrderDao orderDao;
private ItemDao itemDao;
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
public void setItemDao(ItemDao itemDao) {
this.itemDao = itemDao;
public void insertOrder(Order order) {
Transaction Managers (1)
 Native transaction managers
JDBC: DataSourceTransactionManager
Hibernate: HibernateTransactionManager
TopLink: TopLinkTransactionManager
JDO: JdoTransactionManager
 Native strategies work in any environment
в–« but only against a single database!
• no distributed transaction coordination
в–« leverage full power of underlying resource
• isolation levels, savepoints, etc
Transaction Managers (2)
 JTA-based transaction coordinator
в–« Spring's JtaTransactionManager adapter
в–« using the Java Transaction API to talk to an
external transaction coordinator
• typically using the standard XA protocol to coordinate
heterogeneous transactional resources
 Typically: J2EE server's JTA subsystem
в–« delegates to JTA UserTransaction
• a standard J2EE component, available from JNDI
в–« optionally accesses JTA TransactionManager
• server-specific, for transaction suspension
• automatically detected on most J2EE servers
Transaction Managers (3)
 Special support for extended JTA
в–« on specific J2EE servers
в–« through special JtaTransactionManager subclasses
 WebLogicJtaTransactionManager
в–« support for WebLogic's JTA extensions
• transaction names for WebLogic's transaction monitor
(part of the administration console)
• enforced transaction resume in all cases
• per-transaction isolation levels
в–« runs on WebLogic 7.0+
 Support for other extensions on the roadmap
Transaction Managers (4)
 Transaction manager is a deployment choice
в–« application code is completely decoupled from
concrete transaction infrastructure
в–« application specifies desired transaction semantics;
transaction manager provides them at runtime
 Seamless switching between native
transactions and JTA transactions
в–« between non-J2EE and J2EE environments
• e.g.: run integration tests with native transactions
• e.g.: run with J2EE transactions in production
в–« or even run with native transactions in production
• if all you need is transactions for a single database
Transaction Managers (5)
 Typical scenarios for JDBC
в–« standalone application / integration tests
• native JDBC DataSourceTransactionManager
в–« J2EE web application running on Tomcat
• native JDBC DataSourceTransactionManager
в–« J2EE web application running on WebLogic
• J2EE transactions via standard JtaTransactionManager
• or WebLogicJtaTransactionManager for advanced support
 Could also use third-party JTA provider
в–« for standalone application or Tomcat
в–« for example: ObjectWeb JOTM
• or standalone Geronimo transaction manager
Data Access (1)
 Data access objects need to access
transactional resources
в–« as seamlessly as possible
• easy access to scoped transactional resource
в–« automatically participate in transactions
• ideally: run non-transactionally else
 JDBC: talk to transactional DataSource
в–« returns active Connection for current transaction
в–« for example: J2EE server DataSource
• fetched from JNDI via JndiObjectFactoryBean
в–« or transactional DataSource proxy
• for local connection pool
Data Access (2)
 Two different approaches
в–« use native resource API only
в–« or use Spring's DAO implementation helpers
 Native JDBC API
в–« talk to provided transactional DataSource
 Native Hibernate API
в–« use SessionFactory.getCurrentSession() on
provided SessionFactory
 Native TopLink API
в–« use Session.getActiveUnitOfWork() on
provided Session
Data Access (3)
 Spring provides DAO implementation helpers
convenient template classes
implicit access to resources
many operations become one-liners
no try/catch blocks anymore
 Pre-built integration classes for different APIs
JDBC: JdbcTemplate
Hibernate: HibernateTemplate
TopLink: TopLinkTemplate
JDO: JdoTemplate
Data Access (4)
 Further benefit: common DAO exceptions
в–« well-defined exceptions at DAO interface level
в–« do not couple caller to DAO implementation strategy
• even if specific exception conditions need to be handled
 Spring's DataAccessException hierarchy
в–« independent of JDBC, Hibernate, TopLink, JDO, etc
в–« unchecked (= a RuntimeException), as most data
access failures are not recoverable
в–« subclasses like OptimisticLockingFailureException
and DataAccessResourceFailureException
Data Access (5)
 Example for a DAO based on Spring's
public class ExampleDao extends JdbcDaoSupport {
public void clearDatabase() throws DataAccessException {
getJdbcTemplate().update("DELETE FROM imagedb");
public void deleteImage(int imageId) throws DataAccessException {
getJdbcTemplate().update("DELETE FROM imagedb WHERE id=?",
new Object[] {new Integer(imageId)});
public int getNrOfImages() throws DataAccessException {
return getJdbcTemplate().queryForInt(
"SELECT COUNT(*) FROM imagedb");
Data Access (6)
 Example for a DAO based on Spring's
public class ExampleHibernateDao extends HibernateDaoSupport {
public List getVets() throws DataAccessException {
return getHibernateTemplate().find(
"from Vet vet order by vet.lastName, vet.firstName");
public List findOwners(String lastName) throws DataAccessException {
return getHibernateTemplate().find(
"from Owner owner where owner.lastName like ?", lastName + "%");
public Owner loadOwner(int id) throws DataAccessException {
return getHibernateTemplate().load(Owner.class, new Integer(id));
Data Access (7)
 Alternative: a plain Hibernate DAO
public class ExampleHibernateDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
public List getVets() {
return this.sessionFactory.getCurrentSession().
createQuery("from Vet vet order by vet.lastName, vet.firstName").
public Owner loadOwner(int id) {
return this.sessionFactory.getCurrentSession().
load(Owner.class, new Integer(id));
ORM Transactions (1)
 Object/Relational Mapping tools require
proper transaction integration
в–« synchronization with transactions
• cache callbacks on commit / rollback
• required for transactional cache handling
в–« scoping of ORM resources
one Hibernate Session per transaction
one TopLink UnitOfWork per transaction
one JDO PersistenceManager per transaction
required for proper transaction isolation
ORM Transactions (2)
 ORM tools usually support two different
transaction modes
в–« native transactions through ORM API
в–« participating in global JTA transactions
в–« -> different programming model!
 Spring supports consistent transaction
demarcation across all environments
в–« native transactions or global JTA
в–« same transaction demarcation
в–« same DAO implementation model
ORM Transactions (3)
 Spring provides pre-built integration for
all major ORM providers
Hibernate 2.1 / 3.x
TopLink 9.0.4 / 10.1.3
JDO 1.0 / 2.0
Apache OJB 1.0
 Set of integration classes per strategy
в–« factories for ORM resources
в–« native transaction managers
в–« DAO implementation helpers
ORM Transactions (4)
 Example: Hibernate
в–« Spring's LocalSessionFactoryBean
• setup of Hibernate SessionFactory
в–« Spring's HibernateTransactionManager
• native transactions against single database
в–« Spring's HibernateTemplate
• implicit management of Hibernate Sessions
• or use SessionFactory.getCurrentSession()
в–« Spring's OpenSessionInViewFilter
• seamless lazy loading during view rendering
ORM Transactions (5)
 Example: TopLink
в–« Spring's LocalSessionFactoryBean
• setup of TopLink SessionFactory /
TopLink master Session
в–« Spring's TopLinkTransactionManager
• native transactions against single database
в–« Spring's TopLinkTemplate
• implicit management of TopLink Session /
• or use Session.getActiveSession() /
ORM Transactions (6)
 Newest kid on the block: JPA
в–« Java Persistence API 1.0
• aka "EJB3 persistence" / "JSR-220 persistence"
• part of the forthcoming J2EE 5.0
в–« optimized for application servers
• explicit, strong integration with J2EE transactions
• but also usable in a standalone fashion
 Spring will support JPA persistence
в–« as further ORM strategy, analogous to JDO
в–« use Spring's transaction management
with JPA-based DAOs
ORM Transactions (7)
 Example for a plain JPA DAO, using the
"shared EntityManager proxy" model
public class ExampleJpaDao {
private EntityManager entityManager;
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
public List getVets() {
return this.entityManager.createQuery(
"from Vet vet order by vet.lastName, vet.firstName").
public Owner loadOwner(int id) {
return this.entityManager.find(Owner.class, new Integer(id));
Summary (1)
 Spring provides generic transaction
management for any kind of Java application
в–« consistent programming model
в–« consistent transaction demarcation
в–« seamless switching between different transaction
 Declarative transaction demarcation
в–« XML-based proxy definitions
в–« JDK 5.0 annotations
в–« alternative: programmatic demarcation
Summary (2)
 Seamless switching between native
transactions and JTA transactions
в–« consistent programming model across different
deployment scenarios
в–« leverage J2EE server when available,
while still being able to run outside of J2EE
 Full declarative transactions even on Tomcat!
в–« on the other end of the spectrum:
WebLogic JTA extensions supported as well
в–« choose deployment environment according to your
service needs (scaling both up and down)
Summary (3)
 Explicit support for ORM tools
в–« transaction synchronization
• scoping of ORM resources
в–« consistent transaction demarcation across
native transactions and JTA transactions
• with consistent ORM semantics
 All major ORM providers supported
в–« Hibernate, TopLink, JDO, JPA, etc
в–« integration comes out-of-the-box!
• maintained by Spring team
Размер файла
913 Кб
Пожаловаться на содержимое документа