Save This Page
Home » spring-framework-2.5.6-with-dependencies » org.springframework » orm » jpa » [javadoc | source]
org.springframework.orm.jpa
public class: JpaTransactionManager [javadoc | source]
java.lang.Object
   org.springframework.transaction.support.AbstractPlatformTransactionManager
      org.springframework.orm.jpa.JpaTransactionManager

All Implemented Interfaces:
    InitializingBean, ResourceTransactionManager, PlatformTransactionManager, Serializable

org.springframework.transaction.PlatformTransactionManager implementation for a single JPA javax.persistence.EntityManagerFactory . Binds a JPA EntityManager from the specified factory to the thread, potentially allowing for one thread-bound EntityManager per factory. SharedEntityManagerCreator and JpaTemplate are aware of thread-bound entity managers and participate in such transactions automatically. Using either is required for JPA access code supporting this transaction management mechanism.

This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through org.springframework.transaction.jta.JtaTransactionManager ) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions.

This transaction manager also supports direct DataSource access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services which access JPA and services which use plain JDBC (without being aware of JPA)! Application code needs to stick to the same simple Connection lookup pattern as with org.springframework.jdbc.datasource.DataSourceTransactionManager (i.e. org.springframework.jdbc.datasource.DataSourceUtils#getConnection or going through a org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy ). Note that this requires a vendor-specific JpaDialect to be configured.

Note: To be able to register a DataSource's Connection for plain JDBC code, this instance needs to be aware of the DataSource (#setDataSource ). The given DataSource should obviously match the one used by the given EntityManagerFactory. This transaction manager will autodetect the DataSource used as known connection factory of the EntityManagerFactory, so you usually don't need to explicitly specify the "dataSource" property.

On JDBC 3.0, this transaction manager supports nested transactions via JDBC 3.0 Savepoints. The #setNestedTransactionAllowed "nestedTransactionAllowed"} flag defaults to "false", though, as nested transactions will just apply to the JDBC Connection, not to the JPA EntityManager and its cached objects. You can manually set the flag to "true" if you want to use nested transactions for JDBC access code which participates in JPA transactions (provided that your JDBC driver supports Savepoints). Note that JPA itself does not support nested transactions! Hence, do not expect JPA access code to semantically participate in a nested transaction.

Fields inherited from org.springframework.transaction.support.AbstractPlatformTransactionManager:
SYNCHRONIZATION_ALWAYS,  SYNCHRONIZATION_ON_ACTUAL_TRANSACTION,  SYNCHRONIZATION_NEVER,  logger
Constructor:
 public JpaTransactionManager() 
 public JpaTransactionManager(EntityManagerFactory emf) 
    Create a new JpaTransactionManager instance.
    Parameters:
    emf - EntityManagerFactory to manage transactions for
Method from org.springframework.orm.jpa.JpaTransactionManager Summary:
afterPropertiesSet,   closeEntityManagerAfterFailedBegin,   createEntityManagerForTransaction,   doBegin,   doCleanupAfterCompletion,   doCommit,   doGetTransaction,   doResume,   doRollback,   doSetRollbackOnly,   doSuspend,   getDataSource,   getEntityManagerFactory,   getJpaDialect,   getJpaPropertyMap,   getResourceFactory,   isExistingTransaction,   setDataSource,   setEntityManagerFactory,   setJpaDialect,   setJpaProperties,   setJpaPropertyMap,   shouldCommitOnGlobalRollbackOnly
Methods from org.springframework.transaction.support.AbstractPlatformTransactionManager:
commit,   determineTimeout,   doBegin,   doCleanupAfterCompletion,   doCommit,   doGetTransaction,   doResume,   doRollback,   doSetRollbackOnly,   doSuspend,   getDefaultTimeout,   getTransaction,   getTransactionSynchronization,   invokeAfterCompletion,   isExistingTransaction,   isFailEarlyOnGlobalRollbackOnly,   isGlobalRollbackOnParticipationFailure,   isNestedTransactionAllowed,   isRollbackOnCommitFailure,   isValidateExistingTransaction,   newTransactionStatus,   prepareForCommit,   registerAfterCompletionWithExistingTransaction,   resume,   rollback,   setDefaultTimeout,   setFailEarlyOnGlobalRollbackOnly,   setGlobalRollbackOnParticipationFailure,   setNestedTransactionAllowed,   setRollbackOnCommitFailure,   setTransactionSynchronization,   setTransactionSynchronizationName,   setValidateExistingTransaction,   shouldCommitOnGlobalRollbackOnly,   suspend,   triggerBeforeCommit,   triggerBeforeCompletion,   useSavepointForNestedTransaction
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.orm.jpa.JpaTransactionManager Detail:
 public  void afterPropertiesSet() 
    Eagerly initialize the JPA dialect, creating a default one for the specified EntityManagerFactory if none set. Auto-detect the EntityManagerFactory's DataSource, if any.
 protected  void closeEntityManagerAfterFailedBegin(JpaTransactionManager.JpaTransactionObject txObject) 
    Close the current transaction's EntityManager. Called after a transaction begin attempt failed.
 protected EntityManager createEntityManagerForTransaction() 
    Create a JPA EntityManager to be used for a transaction.

    The default implementation checks whether the EntityManagerFactory is a Spring proxy and unwraps it first.

 protected  void doBegin(Object transaction,
    TransactionDefinition definition) 
 protected  void doCleanupAfterCompletion(Object transaction) 
 protected  void doCommit(DefaultTransactionStatus status) 
 protected Object doGetTransaction() 
 protected  void doResume(Object transaction,
    Object suspendedResources) 
 protected  void doRollback(DefaultTransactionStatus status) 
 protected  void doSetRollbackOnly(DefaultTransactionStatus status) 
 protected Object doSuspend(Object transaction) 
 public DataSource getDataSource() 
    Return the JDBC DataSource that this instance manages transactions for.
 public EntityManagerFactory getEntityManagerFactory() 
    Return the EntityManagerFactory that this instance should manage transactions for.
 public JpaDialect getJpaDialect() 
    Return the JPA dialect to use for this transaction manager.
 public Map getJpaPropertyMap() 
    Allow Map access to the JPA properties to be passed to the persistence provider, with the option to add or override specific entries.

    Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".

 public Object getResourceFactory() 
 protected boolean isExistingTransaction(Object transaction) 
 public  void setDataSource(DataSource dataSource) 
    Set the JDBC DataSource that this instance should manage transactions for. The DataSource should match the one used by the JPA EntityManagerFactory: for example, you could specify the same JNDI DataSource for both.

    If the EntityManagerFactory uses a known DataSource as connection factory, the DataSource will be autodetected: You can still explictly specify the DataSource, but you don't need to in this case.

    A transactional JDBC Connection for this DataSource will be provided to application code accessing this DataSource directly via DataSourceUtils or JdbcTemplate. The Connection will be taken from the JPA EntityManager.

    Note that you need to use a JPA dialect for a specific JPA implementation to allow for exposing JPA transactions as JDBC transactions.

    The DataSource specified here should be the target DataSource to manage transactions for, not a TransactionAwareDataSourceProxy. Only data access code may work with TransactionAwareDataSourceProxy, while the transaction manager needs to work on the underlying target DataSource. If there's nevertheless a TransactionAwareDataSourceProxy passed in, it will be unwrapped to extract its target DataSource.

 public  void setEntityManagerFactory(EntityManagerFactory emf) 
    Set the EntityManagerFactory that this instance should manage transactions for.
 public  void setJpaDialect(JpaDialect jpaDialect) 
    Set the JPA dialect to use for this transaction manager. Used for vendor-specific transaction management and JDBC connection exposure.

    If the EntityManagerFactory uses a known JpaDialect, it will be autodetected: You can still explictly specify the DataSource, but you don't need to in this case.

    The dialect object can be used to retrieve the underlying JDBC connection and thus allows for exposing JPA transactions as JDBC transactions.

 public  void setJpaProperties(Properties jpaProperties) 
    Specify JPA properties, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

    Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.

 public  void setJpaPropertyMap(Map jpaProperties) 
    Specify JPA properties as a Map, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

    Can be populated with a "map" or "props" element in XML bean definitions.

 protected boolean shouldCommitOnGlobalRollbackOnly() 
    This implementation returns "true": a JPA commit will properly handle transactions that have been marked rollback-only at a global level.