quinta-feira, 22 de agosto de 2013

Spring Transaction = Operation all or nothing


From the book:
    Spring in Action 3rd Edition.
    Link to it-ebooks to Download

- Transaction = Operation all or nothing

    Transaction group several operations into a single unit of work that fully happens or fully doesn't happen.
    A Transaction should respect the ACID
        (Atomic-single action, Consistency- data shouldn't be corrupted, Isolation-One user per data, Durable- result is permanent)
    Spring suports Programmatics and declarative transaction management.
    Programmatic transaction management
        Doesn't require JTA.
        Can use the persistence mechanism that includes JDBC, Hibernate and JPA.
        Spring suports distributed transactions(XA) using third-party JTA implementation.
        Fine-grained control over transaction boundaries(decide when to begin and end)
        Approach 1
            adds boundaries in the method using TransactionTemplate(uses the callback mechanism
            class SpitterServiceImpl {
                public void saveSpittle(final Spittle spittle){
                    txTemplate.execute(new TransactionCallback<Void>(){  <--- Implement TransactionCallback
                        public void doInTransaction(TransactionStatus txStatus){
                            try{
                                spitterDao.saveSpittle(spittle);
                            } catch(RuntimeExceptione){
                                txStatus.setRollbackOnly();
                                throw e;
                            }
                            return null;
                        }
                    });
                }
            }
            Transaction Template comes from:
            <bean id="spitterService" class="com.habuma.spitter.service.SpitterServiceImpl">
                ...
                <property name="transactionTemplate">
                    <bean class="org.springframework.transaction.support.TransactionTemplate">
                        <property name="transactionManager" ref="transactionManager" />
                    </bean>
                </property>
            </bean>
    Declarative transaction management
        Implemented through AOP and help user decouple an operation from its transaction rules.
        Allow the definition of transaction boundaries declaratively. Also, additional attibutes.
            Propagation
                Boundaries to respect to the client and to the method being called.
                7 progapagin behaviors
                    PROPAGATION_MANDATORY - Method must run within a transaction
                    PROPAGATION_NESTED - Method should run within a nested transaction
                    PROPAGATION_NEVER - Method should not run within a transaction
                    etc
            Isolation levels
                Defines how much a transaction may be impacted by the activities of other concurrent transactions.
                Try to solve the following problems:
                    Dirty read: when one transaction reads data that has been written but not yet commited by another transaction.
                    Nonrepeatable reads: when a transaction performs the same query two times and each time the data is different.
                    Phanton reads: Similar to above.
                Isolation affects performance.  For this reason there are levels. Not all applications requieres perfect isolation.
                Ex of levels:
                    ISOLATION_DEFAULT: uses default isolation level
                    ISOLATION_READ_UNCOMMITED: Allows read changes that haven't committed.  May result in dirty reads, etc
                    ISOLATION_READ_COMMITED: Allow read changes that have been committed. Prevend dirty reads
                    ISOLATION_REPEATABLE_READ: Multiple reads implies the same results. Stil phantom.
                    ISOLATION_SERIALIZABLE: ACID.  Slowest isolation level.
            Read-only hints
                Whether the transaction is read-only.  Data store may be able to apply some optimizations.
            Timeout
                To avoid long-running locks and automatically roll back after some seconds.
            rollback rules
                Can allow roll back on specific checked exceptions or runtime exceptions.

        <tx:adviceid="txAdvice">
            <tx:attributes>
                <tx:methodname="add*"propagation="REQUIRED"/>
                <tx:methodname="*"propagation="SUPPORTS"
                read-only="true"/>
            </tx:attributes>
        </tx:advice>
               
        More convenience.  Declare the transactions in the context definition file.
        Defining annotation-driven transactions
            In the configuration XML file
                <tx:annotation-driven/> <--- tells Spring to examine all beans in the application context that are annotated with @Transactional(class or method level)
                Ex:
                    @Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
                    public class SpitterServiceImpl implements SpitterService{
                        ...
                        @Transactional(propagation=Propagation.REQUIRED,readOnly=false)
                        public void addSpitter(Spitter spitter){
                            ...
                        }
                    ...
                    }
    To use transaction manager, it is needed to declare it in the applicatoin context. Configuration:
        JDBC transactions
            <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource"ref="dataSource"/>
            </bean>
            dataSouce is set with a reference to a bean named dataSoruce. dataSource bean is a javax.sql.DataSource defined in context definition file.
            DataSourceTransactionManager manages transaction by making calls on the java.sql.Connection object retrieved from the DataSource.
            DataSourceTransactionManager calls commit() or rollback() on Connection object.
        Hibernate transactions (HibernateTransactionManager)
            <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <propertyname="sessionFactory"ref="sessionFactory"/> <- sessionFactory is wired with a Hibernate SessionFactory
            </bean>
            HibernateTransactionManager delegates responsibility for transaction management to org.hibernate.Transaction retrieved from Hibernate session.
            HibernateTransactionManager call commit() or rollback() on Transaction obj

Nenhum comentário:

Postar um comentário