Skip to content

Commit 42c9f8e

Browse files
vonzhouvonzhou
vonzhou
authored and
vonzhou
committed
谁怕?一蓑烟雨任平生
1 parent 4b34bf1 commit 42c9f8e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1964
-293
lines changed

.idea/compiler.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 439 additions & 293 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,21 @@
6363

6464
[spitter-persistence-jpa](https://github.com/vonzhou/SpringInAction3/tree/master/spitter-persistence-jpa)
6565

66+
## 6. 事务管理
67+
68+
* 事务的ACID特性
69+
* 编码式 vs 声明式事务在细粒度和易用性之间的权衡
70+
* Spring为每种场景定义了事务管理器(TransactionManager),底层是特定的事务实现
71+
* 要理解JDBC事务,Hibernate事务幕后的实现
72+
* 声明式事务的5个属性:传播行为,隔离级别,只读,超时,回滚规则
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
6683

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<module>spitter-persistence-jdbc-template</module>
2121
<module>spitter-persistence-hibernate-contextual-session</module>
2222
<module>spitter-persistence-jpa</module>
23+
<module>spitter-service-programmatic-tx</module>
24+
<module>spitter-service-declarative-tx</module>
2325
</modules>
2426

2527

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<repositories>
8+
<repository>
9+
<id>GlassFish</id>
10+
<name>GlassFish Maven Repository</name>
11+
<url>http://download.java.net/maven/glassfish/</url>
12+
</repository>
13+
<repository>
14+
<id>java.net.m1repo</id>
15+
<name>java.net Maven 1 Repository</name>
16+
<url>http://download.java.net/maven/1/</url>
17+
<layout>legacy</layout>
18+
</repository>
19+
<repository>
20+
<id>java.net.m2repo</id>
21+
<name>java.net Maven 2 Repository</name>
22+
<url>http://download.java.net/maven/2/</url>
23+
<layout>default</layout>
24+
</repository>
25+
</repositories>
26+
27+
<groupId>com.vonzhou.springinaction3</groupId>
28+
<artifactId>spitter-persistence-hibernate-contextual-session</artifactId>
29+
<version>1.0-SNAPSHOT</version>
30+
31+
<properties>
32+
<spring.group>org.springframework</spring.group>
33+
<spring.version>4.2.5.RELEASE</spring.version>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>${spring.group}</groupId>
39+
<artifactId>spring-context</artifactId>
40+
<version>${spring.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>${spring.group}</groupId>
44+
<artifactId>spring-aop</artifactId>
45+
<version>${spring.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>${spring.group}</groupId>
49+
<artifactId>spring-jdbc</artifactId>
50+
<version>${spring.version}</version>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>mysql</groupId>
55+
<artifactId>mysql-connector-java</artifactId>
56+
<version>5.1.34</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.springframework</groupId>
61+
<artifactId>spring-orm</artifactId>
62+
<version>4.2.5.RELEASE</version>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>org.hibernate</groupId>
67+
<artifactId>hibernate-core</artifactId>
68+
<version>5.0.7.Final</version>
69+
</dependency>
70+
71+
<dependency>
72+
<groupId>org.hibernate</groupId>
73+
<artifactId>hibernate-annotations</artifactId>
74+
<version>3.5.6-Final</version>
75+
</dependency>
76+
77+
<dependency>
78+
<groupId>org.aspectj</groupId>
79+
<artifactId>aspectjweaver</artifactId>
80+
<version>1.6.4</version>
81+
</dependency>
82+
83+
84+
85+
</dependencies>
86+
87+
88+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.2.5.RELEASE" level="project" />
15+
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.2.5.RELEASE" level="project" />
16+
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.2.5.RELEASE" level="project" />
17+
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
18+
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.2.5.RELEASE" level="project" />
19+
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.2.5.RELEASE" level="project" />
20+
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
21+
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:4.2.5.RELEASE" level="project" />
22+
<orderEntry type="library" name="Maven: org.springframework:spring-tx:4.2.5.RELEASE" level="project" />
23+
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.34" level="project" />
24+
<orderEntry type="library" name="Maven: org.springframework:spring-orm:4.2.5.RELEASE" level="project" />
25+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:5.0.7.Final" level="project" />
26+
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.0.Final" level="project" />
27+
<orderEntry type="library" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final" level="project" />
28+
<orderEntry type="library" name="Maven: org.javassist:javassist:3.18.1-GA" level="project" />
29+
<orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
30+
<orderEntry type="library" name="Maven: org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1" level="project" />
31+
<orderEntry type="library" name="Maven: org.jboss:jandex:2.0.0.Final" level="project" />
32+
<orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
33+
<orderEntry type="library" name="Maven: xml-apis:xml-apis:1.0.b2" level="project" />
34+
<orderEntry type="library" name="Maven: org.hibernate.common:hibernate-commons-annotations:5.0.1.Final" level="project" />
35+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-annotations:3.5.6-Final" level="project" />
36+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-commons-annotations:3.2.0.Final" level="project" />
37+
<orderEntry type="library" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final" level="project" />
38+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.5.8" level="project" />
39+
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.6.4" level="project" />
40+
</component>
41+
</module>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//<start id="java_contextualHibernateDao"/>
2+
package com.vonzhou.spitter.persistence;
3+
4+
import org.hibernate.Session;
5+
import org.hibernate.SessionFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Repository;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import java.util.List;
11+
12+
13+
@Repository
14+
@Transactional
15+
public class HibernateSpitterDao implements SpitterDao {
16+
private SessionFactory sessionFactory;
17+
18+
@Autowired
19+
public HibernateSpitterDao(SessionFactory sessionFactory) {//<co id="co_injectSessionFactory"/>
20+
this.sessionFactory = sessionFactory;
21+
}
22+
23+
private Session currentSession() {
24+
return sessionFactory.getCurrentSession();//<co id="co_getCurrentSession"/>
25+
}
26+
27+
public void addSpitter(Spitter spitter) {
28+
currentSession().save(spitter);//<co id="co_useSession"/>
29+
}
30+
31+
public Spitter getSpitterById(long id) {
32+
return (Spitter) currentSession().get(Spitter.class, id);//<co id="co_useSession"/>
33+
}
34+
35+
public void saveSpitter(Spitter spitter) {
36+
currentSession().update(spitter);//<co id="co_useSession"/>
37+
}
38+
//<end id="java_contextualHibernateDao"/>
39+
40+
public List<Spittle> getRecentSpittle() {
41+
// return currentSession().f loadAll(Spittle.class); // this isn't right...just a placeholder for now
42+
return null;
43+
}
44+
45+
public void saveSpittle(Spittle spittle) {
46+
currentSession().save(spittle);
47+
}
48+
49+
public List<Spittle> getSpittlesForSpitter(
50+
Spitter spitter) {
51+
// TODO Auto-generated method stub
52+
return null;
53+
}
54+
55+
public Spitter getSpitterByUsername(String username) {
56+
// TODO Auto-generated method stub
57+
return null;
58+
}
59+
60+
public void deleteSpittle(long id) {
61+
currentSession().delete(getSpittleById(id));
62+
}
63+
64+
public Spittle getSpittleById(long id) {
65+
return (Spittle) currentSession().get(Spittle.class, id);
66+
}
67+
68+
public List<Spitter> findAllSpitters() {
69+
// TODO Auto-generated method stub
70+
return null;
71+
}
72+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.vonzhou.spitter.persistence;
2+
3+
import java.util.List;
4+
5+
public class Spitter {
6+
private Long id;
7+
private String username;
8+
private String password;
9+
private String fullName;
10+
private List<Spittle> spittles;
11+
private String email;
12+
private boolean updateByEmail;
13+
14+
public Long getId() {
15+
return id;
16+
}
17+
18+
public void setId(Long id) {
19+
this.id = id;
20+
}
21+
22+
public String getUsername() {
23+
return this.username;
24+
}
25+
26+
public void setUsername(String username) {
27+
this.username = username;
28+
}
29+
30+
public String getPassword() {
31+
return this.password;
32+
}
33+
34+
public void setPassword(String password) {
35+
this.password = password;
36+
}
37+
38+
public void setFullName(String fullName) {
39+
this.fullName = fullName;
40+
}
41+
42+
public String getFullName() {
43+
return fullName;
44+
}
45+
46+
public void setEmail(String email) {
47+
this.email = email;
48+
}
49+
50+
public String getEmail() {
51+
return email;
52+
}
53+
54+
public void setSpittles(List<Spittle> spittles) {
55+
this.spittles = spittles;
56+
}
57+
58+
public List<Spittle> getSpittles() {
59+
return spittles;
60+
}
61+
62+
public void setUpdateByEmail(boolean updateByEmail) {
63+
this.updateByEmail = updateByEmail;
64+
}
65+
66+
public boolean isUpdateByEmail() {
67+
return updateByEmail;
68+
}
69+
70+
71+
@Override
72+
public boolean equals(Object obj) {
73+
Spitter other = (Spitter) obj;
74+
return other.fullName.equals(fullName) && other.username.equals(username) && other.password.equals(password);
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
// TODO Auto-generated method stub
80+
return super.hashCode();
81+
}
82+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.vonzhou.spitter.persistence;
2+
3+
4+
import java.util.List;
5+
6+
7+
public interface SpitterDao {
8+
void addSpitter(Spitter spitter);
9+
10+
void saveSpitter(Spitter spitter);
11+
12+
Spitter getSpitterById(long id);
13+
14+
List<Spittle> getRecentSpittle();
15+
16+
void saveSpittle(Spittle spittle);
17+
18+
List<Spittle> getSpittlesForSpitter(Spitter spitter);
19+
20+
Spitter getSpitterByUsername(String username);
21+
22+
void deleteSpittle(long id);
23+
24+
Spittle getSpittleById(long id);
25+
26+
List<Spitter> findAllSpitters();
27+
}

0 commit comments

Comments
 (0)