ApplicationContext.Xml 配置文件
Web.xml 配置文件
Attendance 类
CheckWiner 代理代理调用 Attendance 类
[code]
<property name="defaultAutoCommit" value="false" />
</bean>
//这行代码表示是否自动提交的,所以 修改下 values="true" 就ok了
[/code]
<?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://ww[align=center][/align]w.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url"
value="jdbc:jtds:sqlserver://localhost:1433/test;charset=GBK" />
<property name="username" value="sa" />
<property name="password" value="123" />
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- jdbcTemplate 注入 -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- attendance 类注入 -->
<bean id="attendance" class="emisx.attend.Attendance">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- 代理类 -->
<bean id="checkWiner" class="emisx.attend.CheckWiner">
<property name="attendance" ref="attendance"/>
</bean>
<!-- 定时器 -->
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="delay" value="0"/>
<property name="period" value="5000"/>
<property name="timerTask" ref="checkWiner"/>
</bean>
<!-- 周期任务 -->
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledTask" />
</list>
</property>
</bean>
</beans>
Web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Attendance 类
package emisx.attend;
import org.springframework.jdbc.core.JdbcTemplate;
public class Attendance {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void importDBOnThread() {
try {
System.out.println(getJdbcTemplate().hashCode());
System.out.println(getJdbcTemplate().getDataSource());
System.out.println(getJdbcTemplate().toString());
getJdbcTemplate().update("insert into myt values(?,?)",new String[] {"a","n"});
// 执行getJdbcTemplate().update()之后既没有报错,数据库也没有新的纪录不知道怎么回事
System.out.println(getJdbcTemplate().hashCode());
System.out.println(getJdbcTemplate().getDataSource());
System.out.println(getJdbcTemplate().toString());
System.out.println("1111111111");
} catch (Exception e) {
e.printStackTrace();
}
}
}
CheckWiner 代理代理调用 Attendance 类
package emisx.attend;
import java.util.TimerTask;
public class CheckWiner extends TimerTask {
private Attendance attendance;
public Attendance getAttendance() {
return attendance;
}
public void setAttendance(Attendance attendance) {
this.attendance = attendance;
}
public void run() {
try {
attendance.importDBOnThread();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行 attendance.importDBOnThread() 之后的结果
30633470
org.apache.commons.dbcp.BasicDataSource@186c730
org.springframework.jdbc.core.JdbcTemplate@1d36dfe
30633470
org.apache.commons.dbcp.BasicDataSource@186c730
org.springframework.jdbc.core.JdbcTemplate@1d36dfe
1111111111
但是数据库却没有改变。
事务添加或者不添加结果都一样。大家帮帮忙瞧瞧...
[code]
<property name="defaultAutoCommit" value="false" />
</bean>
//这行代码表示是否自动提交的,所以 修改下 values="true" 就ok了
[/code]