The chaos of the joint services operation
The Spring abstract DAO system is compatible with a variety of data access technologies that are unique and different. Like Hibernate is a very good ORM implementation, but the control of the underlying SQL is not convenient, while iBatis through templating technology to allow you to easily control the SQL, but not as high as Hibernate development efficiency; The highest degree of freedom is of course direct use of Spring JD BC mo Genus, but it is also the lowest level, flexible price is the complexity of the code. It's hard to say which data access technology is the best, and only in a particular scenario can the answer be given. So in an application, often use a number of data access technology: Generally two, one using ORM technology framework, and another using the bottom of the JDBC technology, the two perfect combination, forming a joint military services, common resist.
But how does this joint service deal with the problem of managing the affairs? We know that Spring provides the corresponding transaction manager for each data access technology, does it need to configure the corresponding transaction manager separately? How exactly do they collaborate and how do they work? These emerging issues often stifle the idea of developers using the joint service.
In fact, on this issue, we underestimate the ability of Spring transaction management. If you are using a high-end ORM technology (HIBERNATE,JPA, JDO) while using a JDBC technology (Spring Jdbc,ibatis), because the former session is an encapsulation of the latter connection (Connection), Spring will be "smart enough" to encapsulate the latter's session with the same transaction thread. So, we just have to take the former transaction manager directly. The following table shows the transaction manager for mixed data access technologies:
Table 1. Transaction manager with mixed data access technology
| mixed data access technology |
transaction manager /td> |
| orm technical framework |
JDBC Technical framework |
| hibernate |
spring J DBC or IBatis |
hibernatetransactionmanager |
| JPA |
spring JDBC or IBatis |
jpatransac Tionmanager |
| spr ing JDBC or iBatis |
jdotransactionmanager |
Since there is generally no case for simultaneous use of multiple ORM frameworks (such as Hibernate + JPA), we do not intend to discuss this proposition, focusing only on the ORM Framework + JDBC framework. Hibernate + Spring JDBC may be the most used combination, let's look at the operation of transaction management through an example.
Manifest 1.user.java: Entity classes with annotation declarations
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Id;
import java.io.Serializable;
@Entity
@Table(name="T_USER")
public class User implements Serializable{
@Id
@Column(name = "USER_NAME")
private String userName;
private String password;
private int score;
@Column(name = "LAST_LOGON_TIME")
private long lastLogonTime = 0;
}