环境
pom.xml 包依赖
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-702.jdbc4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
</dependencies>
资源文件夹(src/main/resources) bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/test" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="3" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="5" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="3" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="3" />
<!-- 设置在自动回收超时连接的时候打印连接的超时错误 -->
<property name="logAbandoned" value="true"/>
<!-- 设置自动回收超时连接 -->
<property name="removeAbandoned" value="true"/>
<!-- 超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="10"/>
<!-- 超时等待时间以毫秒为单位 -->
<property name="maxWait" value="100"/>
</bean>
<bean id="personDao" class="com.royzhou.jdbc.PersonDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="personService" class="com.royzhou.jdbc.PersonServiceImpl">
<property name="personDao" ref="personDao"></property>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>
java文件
main方法
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
PersonService ps = (PersonService)ctx.getBean("personService");
int key=ps.addPerson(new PersonBean("jack"));
}
@Transactional //这个必须的
public class PersonServiceImpl implements PersonService {
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
//验证事务的业务方法 ,先新增,然后更新,认为制造异常,然后回滚
public int addPerson(PersonBean person) {
int key = personDao.addPerson(person);
// personDao.deletePerson(key);
person.setId(key);
person.setName("jack"+key);
updatePerson(person);
return 1;
}
}
public class PersonDaoImpl implements PersonDao {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource ds) {
this.jdbcTemplate = new JdbcTemplate(ds);
}
public int addPerson(PersonBean person) {
int key = 0;
KeyHolder holder = new GeneratedKeyHolder();
try {
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
int i = 1;
PreparedStatement ps = con.prepareStatement(
"insert into person(name) values(?)",
new String[] { "id" }
);
ps.setString(i++, "jack" + i);
return ps;
}
},
holder
);
key = holder.getKey().intValue();
}catch (DataAccessException e) {
e.printStackTrace() ;
}
return key;
}
public void updatePerson(PersonBean person) {
jdbcTemplate.update("update person set name = ? where id = ?",
new Object[] { person.getName(), person.getId() }, new int[] {
Types.VARCHAR, Types.INTEGER });
throw new RuntimeException("运行期例外");
}
}