Spring Integration Hibernate

Source: Internet
Author: User

1.Spring Integrated Hibernate
(1) Introduction of the development package
A. Spring Basic package: Spring.jar,commons-logging.jar
B. Spring AOP Package: Aspectjrt.jar,aspectjweaver.jar,cglib-nodep-2.1_3.jar
C. Database driver package: Mysql-connector-java-5.1.6-bin.jar
D. Connection Pool Package: Commons-dbcp.jar,commons-pool.jar,commons-collections.jar
E. Hibernate:hibernate3.jar,hibernate-entitymanager.jar,hibernate-commons-annotations.jar, Hibernate-annotations.jar
F. DOM4J Development Kit: Dom4j-1.6.1.jar
G. SLF4J Development Kit: Slf4j-api-1.5.0.jar,slf4j-log4j12-1.5.0.jar
H. log4j Development Kit: Log4j-1.2.15.jar
I. Javassist Development Kit: Javassist.jar
J. (optional) If you need to introduce Commons-annotation.jar using spring annotations

2.Spring Transaction Management
Divided into programmatic and declarative transaction management. The project generally uses declarative transaction management.
Spring's declarative transaction management is implemented through spring AOP.
(1) Based on XML configuration method
A. Defining a transaction management bean
----JDBC----
<bean id= "Txmanager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>
-----Hibernate-----
<bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Mysessionfactory" ></property>
</bean>

B. Defining notifications
<tx:advice id= "Txadvice" transaction-manager= "Txmanager" >
<tx:attributes>
<tx:method name= "add*" propagation= "REQUIRED"/>
</tx:attributes>
</tx:advice>

C. Using AOP to cut in
<aop:config>
<aop:pointcut id= "txpointcut" expression= "Bean (*service)"/>
<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Txpointcut"/>
</aop:config>
(2) Transaction policy
required--supports the current transaction and creates a new transaction if there is no current transaction. This is the most common choice.
supports--supports the current transaction and is executed in a non-transactional manner if no transaction is currently in use.
The mandatory--supports the current transaction and throws an exception if there is no current transaction.
requires_new--a new transaction, suspending the current transaction if a transaction is currently present.
The not_supported--executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present.
The never--is executed in a non-transactional manner and throws an exception if a transaction is currently present.
nested--executes within a nested transaction if a transaction is currently present. If there is currently no transaction, do something similar to required.
With multiple savepoint that can be rolled back, internal rollback does not affect external transactions. Only valid for Datasourcetransactionmanager
3. Annotation Mode configuration transactions
A. Enabling transaction annotations in the spring configuration file
<tx:annotation-driven transaction-manager= "Txmanager"/>
B. Using @transactional in components that require transaction control
--can be defined in front of the class, indicating that all business methods in the class take the default transaction control
--If some individual methods do not need to use transaction control, you can use the
@Transactional (propagation=propagation.not_supported)
--By default, the business method encounters a RuntimeException exception before it is rollback,
If you have other types of exceptions that require rollback, you can use
@Transactional (Propagation=propagation.required,rollbackfor=exception.class)




Spring Integration Hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.