Spring 事务管理和mysql乱码解决
Spring把底的事务管理接口操象出来,形成了一套统一的事务管理方式,适用JTA和JDBC,Spring 提供了两种事务管理方式,和种是编程式事务另一种是声明式事务,我不喜欢声明式事务,所以就不用管它了,编程式事务有两种方式实现,一种是使用事务管理模板,另一种是用PlatformTransactinManage,下面类是我写的一个例程,仅供参考的,技术细节需参考相关文档
package
com.spring;
import java.sql.Connection;
import java.sql.Statement;
import javax.sql.DataSource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.orm.toplink.TopLinkTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class TransactionDemo {
/**
* spring事务模板例子
*
*/
public void templateTemplateDemo(){
final ApplicationContext ctx = new ClassPathXmlApplicationContext( " src/bean.xml " );
// ClassPathResource cpr=new ClassPathResource("bean.xml");
// final XmlBeanFactory factory=new XmlBeanFactory(cpr);
PlatformTransactionManager ptm = (PlatformTransactionManager)ctx.getBean( " transactionManager " );
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute( new TransactionCallbackWithoutResult(){
@Override
protected void doInTransactionWithoutResult(TransactionStatus ts) {
try {
DataSource ds = (DataSource)ctx.getBean( " dataSource " );
Connection conn = DataSourceUtils.getConnection(ds);
Statement stmt = conn.createStatement();
stmt.execute( " insert into tuser values(123,'裴德万') " );
} catch (Exception e){
ts.setRollbackOnly();
e.printStackTrace();
}
}
});
}
/**
* 使用PlatforTransactionManager实现类管理事务
*
*/
public void PlatforTransactionManagerDemo(){
ApplicationContext ctx = new ClassPathXmlApplicationContext( " bean.xml " );
PlatformTransactionManager tm = (PlatformTransactionManager)ctx.getBean( " transactionManager " );
DefaultTransactionDefinition dtf = new DefaultTransactionDefinition(); // 初始化一个默认事务
dtf.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); // 设置事务定义对象的事务传达室播属性
TransactionStatus ts = tm.getTransaction(dtf); // TransactionStatus代表事务对象本身,getTransaction开始一个事务
try {
DataSource ds = (DataSource)ctx.getBean( " dataSource " );
Connection conn = DataSourceUtils.getConnection(ds);
Statement stmt = conn.createStatement();
stmt.execute( " insert into tuser values(123,'裴德万') " );
tm.commit(ts);
} catch (Exception e){
tm.rollback(ts);
e.printStackTrace();
}
}
public static void main(String[] args){
TransactionDemo td = new TransactionDemo();
td.templateTemplateDemo();
}
}
对应的XML文件配置(jdbc)如下:
import java.sql.Connection;
import java.sql.Statement;
import javax.sql.DataSource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.orm.toplink.TopLinkTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class TransactionDemo {
/**
* spring事务模板例子
*
*/
public void templateTemplateDemo(){
final ApplicationContext ctx = new ClassPathXmlApplicationContext( " src/bean.xml " );
// ClassPathResource cpr=new ClassPathResource("bean.xml");
// final XmlBeanFactory factory=new XmlBeanFactory(cpr);
PlatformTransactionManager ptm = (PlatformTransactionManager)ctx.getBean( " transactionManager " );
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute( new TransactionCallbackWithoutResult(){
@Override
protected void doInTransactionWithoutResult(TransactionStatus ts) {
try {
DataSource ds = (DataSource)ctx.getBean( " dataSource " );
Connection conn = DataSourceUtils.getConnection(ds);
Statement stmt = conn.createStatement();
stmt.execute( " insert into tuser values(123,'裴德万') " );
} catch (Exception e){
ts.setRollbackOnly();
e.printStackTrace();
}
}
});
}
/**
* 使用PlatforTransactionManager实现类管理事务
*
*/
public void PlatforTransactionManagerDemo(){
ApplicationContext ctx = new ClassPathXmlApplicationContext( " bean.xml " );
PlatformTransactionManager tm = (PlatformTransactionManager)ctx.getBean( " transactionManager " );
DefaultTransactionDefinition dtf = new DefaultTransactionDefinition(); // 初始化一个默认事务
dtf.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); // 设置事务定义对象的事务传达室播属性
TransactionStatus ts = tm.getTransaction(dtf); // TransactionStatus代表事务对象本身,getTransaction开始一个事务
try {
DataSource ds = (DataSource)ctx.getBean( " dataSource " );
Connection conn = DataSourceUtils.getConnection(ds);
Statement stmt = conn.createStatement();
stmt.execute( " insert into tuser values(123,'裴德万') " );
tm.commit(ts);
} catch (Exception e){
tm.rollback(ts);
e.printStackTrace();
}
}
public static void main(String[] args){
TransactionDemo td = new TransactionDemo();
td.templateTemplateDemo();
}
}
<
bean
id
="dataSource"
class
="org.apache.commons.dbcp.BasicDataSource"
destroy-method
="close"
>
< property name ="driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/test?useUnicode=true & characterEncoding=utf-8 </ value >
</ property >
< property name ="username" >
< value > root </ value >
</ property >
< property name ="password" >
< value > 123456 </ value >
</ property >
</ bean >
< property name ="driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/test?useUnicode=true & characterEncoding=utf-8 </ value >
</ property >
< property name ="username" >
< value > root </ value >
</ property >
< property name ="password" >
< value > 123456 </ value >
</ property >
</ bean >
JTA的配置如下
<
bean
id
="transactionManager"
class
="org.springframework.jndi.JndiObjectFactoryBean"
>
< property name ="dataSource" >
< property name ="jndiName" >
< value > jdbc/kkmei </ value >
</ property >
</ bean >
< bean id ="jtatm" class ="org.springframework.transaction.jta.JtaTransactionManager" >
</ bean >
使用ApplicationContext时只能通过applicationContext.xml来初始化容器
< property name ="dataSource" >
< property name ="jndiName" >
< value > jdbc/kkmei </ value >
</ property >
</ bean >
< bean id ="jtatm" class ="org.springframework.transaction.jta.JtaTransactionManager" >
</ bean >
final ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
mysql 乱码问题由来已久,今天发现把my.ini中的字符编码改为utf-8时,mysql无法启动。在这个例子中我们使用了utf-8编码,那么我们要把test数据库的编码,表编码,字段编码全改成utf-8就没问题了,mysql-front停止开发了,郁闷