<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Spring -transaction

    1 Understand Transaction
    ? 1) Introduce Spring's transaction manager
    ? a? JDBC transactions?
    ???? <bean id="transactionManager" class="org.springframework.jdbc.
    ????? datasource.DataSourceTransactionManager">
    ???? <property name="dataSource">
    ???? <ref bean="dataSource"/>
    ???? </property>
    ???? </bean>
    ?? b Hibernate transactions
    ???? <bean id="transactionManager" class="org.springframework.
    ?????? orm.hibernate.HibernateTransactionManager">
    ???? <property name="sessionFactory">
    ???? <ref bean="sessionFactory"/>
    ???? </property>
    ???? </bean>
    2 Programing transaction in Spring
    ?? One approach to adding transaction to your code is to programmly add transactional boundary using transiationTemplate class.
    ?? Programming is good when you want complete control over transactional boundary.but you have to use spring specific class.In most case ,your tansactional needs will not require such precise control over transactional boundaries.That is why you will typically choolse to declare transaction support
    ? public void enrollStudentInCourse() {
    ??? transactionTemplate.execute(
    ??? new TransactionCallback() {
    ????? public Object doInTransaction(TransactionStatus ts) {
    ??????? try {
    ????????? // do stuff?? Runs within doInTransaction()
    ??????? } catch (Exception e) {
    ????????? ts.setRollbackOnly(); //Calls setRollbackOnly() to roll Calls setRollbackOnly()???????????????????????????????? //to roll back
    ??????? }
    ????????? return null;?? //If successful, transaction is committed
    ????? }
    ??? }
    ?? );
    ?}
    ?<bean id="transactionTemplate" class="org.springframework.
    ?????? transaction.support.TransactionTemplate">
    ? <property name="transactionManager">
    ??? <ref bean="transactionManager"/>
    ? </property>
    ?</bean>
    ?<bean id="courseService"
    ??? class="com.springinaction.training.service.CourseServiceImpl">
    ?<property name=" transactionTemplate">
    ???? <ref bean=" transactionTemplate"/>
    ?? </property>
    ?</bean>
    3 Declaring transactions
    ? Spring's support for declarative transaction management is implementedd through Spirng's? AOP framework.
    ? <bean id="courseService" class="org.springframework.transaction.
    ?????? interceptor.TransactionProxyFactoryBean">
    ? <property name="proxyInterfaces">
    ??? <list>
    ????? <value>
    ??????? com.springinaction.training.service.CourseService??
    ????? </value>
    ??? </list>
    ? </property>
    ? <property name="target">
    ?? <ref bean="courseServiceTarget"/>?? //Bean being proxied
    ? </property>
    ?<property name="transactionManager">
    ?? <ref bean="transactionManager"/>?? //Transaction manager
    ?</property>
    ?<property name="transactionAttributeSource">
    ?? <ref bean="attributeSource"/>?? //Transaction attribute source
    ?</property>
    ?</bean>
    ?1) Understanding transaction attributes
    ? In Spring transaction attribute is a description of how transaction policies should be
    applied to a methods
    ?? a? Propagation behavior
    ??? Propagation behavior???????????????? What it means
    ??? PROPAGATION_MANDATORY??????????????? indicate that the method must run within a????????????????????????????????????????????????? transaction.If no transaction is in progress
    ???????????????????????????????????????? an exception will be thrown
    ??? PROPAGATION_NESTED
    ??? PROPAGATION_NEVER??????????????????? indicate that the method can not run withi a??????????????????????????????????????????????? transaction. if a transaction exist an exception??????????????????????????????????????????? will be thrown.
    ??? PROPAGATIOM_NOT_SUPPORT????????????? Indicates that the method should not run within a?????????????????????????????????????????? transaction. If an existing transaction is????????????????????????????????????????????????? in progress, it will be suspended for the
    ???????????????????????????????????????? duration of the method.
    ??? PROPAGATION_REQUIRED???????????????? indicate that the current method must run within a????????????????????????????????????????? transaction.if an existing transaction is in??????????????????????????????????????????????? progress,the ,method will run with the transaction
    ???????????????????????????????????????? otherwise a new transaction will be started
    ??? PROPAGATION_REQUIRENEW?????????????? indicates that the current must run within its own
    ???????????????????????????????????????? transaction.A new transaction is started and an???????????????????????????????????????????? existing transaction will be suspend
    ??? PROPAGATION_SUPPORT????????????????? indicate the current mehtod does not require a????????????????????????????????????????? transaction.but may run if on is already in progress?????
    ??? b Isolation levels?????
    ??? Isolation level??????????????????? What it means
    ??? ISOLATION_DEFAULT????????????????? Using the defaul isolation level of the underlying????????????????????????????????????????? database
    ??? ISOLATION_READ_UNCOMMITTED???????? Allows you read change that have not yet been commit
    ?????????????????????????????????????? May result in dirty read,phantom read,nonrepeatable???????????????????????????????????????? read
    ??? ISOLATION_READ_COMMITTED?????????? Allows reads from concurrent transactions that have
    ?????????????????????????????????????? bean committed.Dirty read are prevent.but platform????????????????????????????????????????? and norepeatable reads may still occur.
    ??? ISOLATIOM_REPEATABLE_READ????????? Multiple read the same field will yield the same??????????????????????????????????????????? result ,unless changed by the transaction?????????????????????????????????????????????????? itself.Dirty reads ,nonrepeatable are all prevented
    ?????????????????????????????????????? phantom may still occur
    ??? ISOLATION_SERIALIZABLE???????????? This fully ACID-compliant isolation level ensusme??????????????????????????????????????? that dirty read,unrepeatable read ,phantom read are??????????????????????????????????????? all prevented.And this is the most slowest isolation
    ?????????????????????????????????????? since it is typically accomplished by doing full??????????????????????????????????????? table lock on the tables in the transaction.

    ?? c Read-Only
    ?? If a transaction performs only read operation against the underlying datastore.when a transaction begin ,it only make sense to declare a transaction as read only on mehtods with
    propagation behavior which start a new transaction.
    ?? Furthermore ,if you are Hibernate as persistence mechanism,declaring a transaction as read only will reult in Hibernate flush mode being set to FLUST_NEVER.this tell hibernate to avoid synchroniztion of objects with database.
    ?? d Transaction timeout
    ?? Suppose that your transaction becomes unexpectedly long-running transaction.Because transaction may invole locks on the underlying database.Instead of waiting it out ,you can delcare a transaction to automaitically roll back.
    ?? because timeout clock begin ticking when a transaction start. it only make sense to declare a transaction timeout on methods with propagation behavior that start a new transaction.
    ? 2) Declaring a simple transaction policy
    ?? <bean id="myTransactionAttribute"
    ??? class="org.springframework.transaction.interceptor.
    ??????????? DefaultTransactionAttribute">
    ? <property name="propagationBehaviorName">
    ??? <value>PROPAGATION_REQUIRES_NEW</value>
    ? </property>
    ? <property name="isolationLevelName">
    ??? <value>ISOLATION_REPEATABLE_READ</value>
    ? </property>
    ?</bean>
    ?<bean id="transactionAttributeSource"
    ??? class="org.springframework.transaction.interceptor.
    ??????????? MatchAlwaysTransactionAttributeSource">
    ? <property name="transactionAttribute">
    ??? <ref bean="myTransactionAttribute"/>
    ? </property>
    ?</bean>
    4 Declaring transactions by method name
    ? 1) Using NameMatchTransactionAttributeSource
    ? The properties property of NameMatchTransactionAttributeSource maps mehtod to a transaction property descriptor. the property descriptor takes the following form:
    ? Propagation,isolation,readOnly,-Exception,+Exception
    ?
    ? <bean id="transactionAttributeSource"
    ??? class="org.springframework.transaction.interceptor.
    ??????????? NameMatchTransactionAttributeSource">
    ? <property name="properties">
    ??? <props>
    ????? <prop key="enrollStudentInCourse">
    ????????? PROPAGATION_REQUIRES_NEW
    ????? </prop>
    ??? </props>
    ? </property>
    ?</bean>
    ?2) Specifying the transaction Isolation level
    ? <bean id="transactionAttributeSource"
    ??? class="org.springframework.transaction.interceptor.
    ??????????? NameMatchTransactionAttributeSource">
    ???? <property name="properties">
    ??? <props>
    ????? <prop key="enrollStudentInCourse">
    ??????? PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ
    ????? </prop>
    ??? </props>
    ? </property>
    ?</bean>
    ?3) Using real-only transaction
    ? <bean id="transactionAttributeSource"
    ??? class="org.springframework.transaction.interceptor.
    ??????????? NameMatchTransactionAttributeSource">
    ? <property name="properties">
    ??? <props>
    ????? <prop key="getCompletedCourses">
    ??????? PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ,readOnly
    ????? </prop>
    ??? </props>
    ? </property>
    </bean>
    ?4)Specifying? rollback rules
    ? You can sepcify that a transaction be rollback on specify checked exception
    ? <bean id="transactionAttributeSource"
    ??? class="org.springframework.transaction.interceptor.
    ??????????? NameMatchTransactionAttributeSource">
    ? <property name="properties">
    ??? <props>
    ????? <prop key="enrollStudentInCourse">
    ??????? PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ,
    ??????? -CourseException
    ??????? </prop>
    ???? </props>
    ??? </property>
    ? </bean>
    ? Exception can be marked as negative(-) or postive(+)
    ? Negative exception will trigger the roll back if the exception (or sublclass of it) is thrown.Postive exception on the other hand indicate that the transacton should be commit
    even if the exception is thrown
    ?5)Using wildcard matches
    ?<bean id="transactionAttributeSource"
    ??? class="org.springframework.transaction.interceptor.
    ??????????? NameMatchTransactionAttributeSource">
    ? <property name="properties">
    ??? <props>
    ????? <prop key="get*">
    ??????? PROPAGATION_SUPPORTS
    ????? </prop>
    ??? </props>
    ? </property>
    </bean>
    ?6 Short-cut name match transaction
    ?<bean id="courseService" class="org.springframework.transaction.
    ?????? interceptor.TransactionProxyFactoryBean">
    ?? <property name="transactionProperties">
    ??? <props>
    ????? <prop key="enrollStudentInCourse">
    ??????? PROPAGATION_REQUIRES_NEW
    ????? </prop>
    ??? </props>
    ?? </property>
    ?</bean>

    posted on 2006-09-15 11:00 康文 閱讀(1807) 評論(0)  編輯  收藏 所屬分類: java

    <2006年9月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    1234567

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲视频在线观看免费| 亚洲国产精品美女| 亚洲熟妇av一区二区三区| 亚洲精品国产精品乱码不卡√| 亚洲AV综合色区无码一区| 亚洲网址在线观看| 亚洲AV无码专区亚洲AV桃| 人禽伦免费交视频播放| 性xxxxx大片免费视频| 国产在线观看片a免费观看| 亚洲阿v天堂在线2017免费| 亚洲Aⅴ无码专区在线观看q| 456亚洲人成在线播放网站| 五月天婷婷免费视频| 国产福利视精品永久免费| 免费jlzzjlzz在线播放视频| 亚洲欧洲免费视频| 国产午夜亚洲精品不卡电影| 人人揉揉香蕉大免费不卡| 免费黄色网址入口| 亚洲综合婷婷久久| 中美日韩在线网免费毛片视频 | 久久精品国产影库免费看| 思思re热免费精品视频66| 亚洲精品国精品久久99热| 亚洲另类古典武侠| 黄桃AV无码免费一区二区三区 | 人妻在线日韩免费视频| 免费观看的a级毛片的网站| 久久精品国产亚洲AV麻豆~| 黄页网址在线免费观看| 中文字幕免费高清视频| 中文字幕亚洲第一| 亚洲AV无码AV吞精久久| 日日麻批免费40分钟日本的| 亚洲无线观看国产精品| 亚洲欧美精品午睡沙发| 无码中文字幕av免费放dvd| 亚洲午夜AV无码专区在线播放 | 久久亚洲成a人片| 一区二区免费在线观看|