1,编程式事务管理(jdbc的事务是绑定在connection上的)
Connection conn = null
try
{
Class.forName("com.mysql.jdbc.Driver")
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","password")
conn.setAutoCommit(false) //取消自动提交
PreparedStatement ps = conn.prepareCall("update something")
ResultSet rs = ps.executeQuery()
conn.commit() //手动提交
}
catch (Exception e)
{
conn.rollback()
e.printStackTrace()
}
finally
{
conn.close()
}
2,声明式事务
先在工程的application.xml配置文件中添加如下代码,开启事务
<!-- 声明式事务控制配置 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="datasource" ref="bassDataSource"></property>
</bean>
然后在你需要开启事务的接口前面添加注解
@Transactional(rollbackFor = IOException.class)
public void add(String name) throws IOException
{
System.out.println("可以再类里和方法里面添加事务注解0~0")
throw new IOException()
}
直接调用接口方法就好
分布式事务处理(mysql貌似在5.X之后才支持) 的话,
1.可以直接使用spring+atomikos框架进行管理
参考:
就不贴测试代码了,自己看着配置吧
2,使用JTA(Java Transaction API)进行分布式事务管理(测试代码如下)
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.SQLException
import javax.naming.InitialContext
import javax.sql.DataSource
import javax.transaction.SystemException
import javax.transaction.UserTransaction
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource
//分布式事务处理
public class transferAccount
{
@SuppressWarnings("null")
public void testTransferAccount()
{
UserTransaction userts = null
Connection connA = null
PreparedStatement psA = null
InitialContext context = null
Connection connB = null
PreparedStatement psB = null
try
{
//获得事务管理对象
userts = (UserTransaction) context.lookup("java:comp/UserTransaction")
//获取两个数据库
connA = getDataSourceA().getConnection()
connB = getDataSourceB().getConnection()
//开启事务
userts.begin()
//sql语句
psA = connA.prepareStatement("我加1")
psB = connB.prepareStatement("我减1")
//执行sql
psA.executeUpdate()
psB.executeUpdate()
//事务提交
userts.commit()
} catch (Exception e)
{
try
{
userts.rollback()
} catch (IllegalStateException | SecurityException
| SystemException e1)
{
e1.printStackTrace()
}
e.printStackTrace()
}
finally
{
try
{
psA.close()
psB.close()
connA.close()
connB.close()
} catch (SQLException e)
{
e.printStackTrace()
}
}
}
public DataSource getDataSourceA()
{
MysqlDataSource dataSource = new MysqlDataSource()
dataSource.setDatabaseName("mysql")
dataSource.setServerName("server")
dataSource.setPortNumber(1433)
dataSource.setUser("test")
dataSource.setPassword("test")
return dataSource
}
public DataSource getDataSourceB()
{
MysqlDataSource dataSource = new MysqlDataSource()
dataSource.setDatabaseName("mysql")
dataSource.setServerName("server")
dataSource.setPortNumber(1435)
dataSource.setUser("test1")
dataSource.setPassword("test1")
return dataSource
}
}
Statement换成preparedStatement,就有相应的set方法了。
或者既然addBatch里面传入的是String类型,那我们自己构造,
stmt.addBatch(“insert into users values ("+"values1"+","+"values2"+")")
或者既然是users类,我们可以根据users中属性是否为初始值来自动生成inset语句,下面是我以前写的代码,仅供参考
注释:
1、下面的User.NAME等就是user中name属性在表中的列名
2、方法ConvertStr就是把插入的列的value加上单引号。
private static String ConvertStr(Object object) {
return "'" + object.toString() + "'"
}
3、其他
public static final String strIns = "insert into users("
public static final String strVal = ") values("
public static final String strEnd = ")"
4、调用
stmt.addBatch(User.InsetStr(user))//这样就不用考虑传参了
代码如下:
public static String InsetStr(User user) {String StrCol = ""
String Values = ""
if (user.getName() != null &&!user.getName().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.NAME
Values = Values + "," + ConvertStr(user.getName())
} else {
StrCol = StrCol + User.NAME
Values = Values + ConvertStr(user.getName())
}
}
if (user.getNick() != null &&!user.getNick().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.NICK
Values = Values + "," + ConvertStr(user.getNick())
} else {
StrCol = StrCol + User.NICK
Values = Values + ConvertStr(user.getNick())
}
}
if (user.getStudentid() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.STUDENTID
Values = Values + "," + ConvertStr(user.getStudentid())
} else {
StrCol = StrCol + User.STUDENTID
Values = Values + ConvertStr(user.getStudentid())
}
}
if (user.getSex() != '\u0000') {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.SEX
Values = Values + "," + ConvertStr(user.getSex())
} else {
StrCol = StrCol + User.SEX
Values = Values + ConvertStr(user.getSex())
}
}
if (user.getPassword() != null &&!user.getPassword().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.PASSWORD
Values = Values + "," + ConvertStr(user.getPassword())
} else {
StrCol = StrCol + User.PASSWORD
Values = Values + ConvertStr(user.getPassword())
}
}
if (user.getHash() != null &&!user.getHash().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.HASH
Values = Values + "," + ConvertStr(user.getHash())
} else {
StrCol = StrCol + User.HASH
Values = Values + ConvertStr(user.getHash())
}
}
if (user.getSchool() != null &&!user.getSchool().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.SCHOOL
Values = Values + "," + ConvertStr(user.getSchool())
} else {
StrCol = StrCol + User.SCHOOL
Values = Values + ConvertStr(user.getSchool())
}
}
if (user.getMajor() != null &&!user.getMajor().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.MAJOR
Values = Values + "," + ConvertStr(user.getMajor())
} else {
StrCol = StrCol + User.MAJOR
Values = Values + ConvertStr(user.getMajor())
}
}
if (user.getMobile() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.MOBILE
Values = Values + "," + ConvertStr(user.getMobile())
} else {
StrCol = StrCol + User.MOBILE
Values = Values + ConvertStr(user.getMobile())
}
}
if (user.getCollege() != null &&!user.getCollege().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.COLLEGE
Values = Values + "," + ConvertStr(user.getCollege())
} else {
StrCol = StrCol + User.COLLEGE
Values = Values + ConvertStr(user.getCollege())
}
}
if (user.getGrade() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.GRADE
Values = Values + "," + ConvertStr(user.getGrade())
} else {
StrCol = StrCol + User.GRADE
Values = Values + ConvertStr(user.getGrade())
}
}
if (user.getBclass() != null &&!user.getBclass().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.BCLASS
Values = Values + "," + ConvertStr(user.getBclass())
} else {
StrCol = StrCol + User.BCLASS
Values = Values + ConvertStr(user.getBclass())
}
}
if (user.getIdnum() != null &&!user.getIdnum().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.IDNUM
Values = Values + "," + ConvertStr(user.getIdnum())
} else {
StrCol = StrCol + User.IDNUM
Values = Values + ConvertStr(user.getIdnum())
}
}
if (user.getEmail() != null &&!user.getEmail().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.EMAIL
Values = Values + "," + ConvertStr(user.getEmail())
} else {
StrCol = StrCol + User.EMAIL
Values = Values + ConvertStr(user.getEmail())
}
}
if (user.getRegip() != null &&!user.getRegip().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.REGIP
Values = Values + "," + ConvertStr(user.getRegip())
} else {
StrCol = StrCol + User.REGIP
Values = Values + ConvertStr(user.getRegip())
}
}
if (user.getRegdate() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.REGDATE
Values = Values + "," + ConvertStr(user.getRegdate())
} else {
StrCol = StrCol + User.REGDATE
Values = Values + ConvertStr(user.getRegdate())
}
}
if (user.getUnit() != null &&!user.getUnit().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.UNIT
Values = Values + "," + ConvertStr(user.getUnit())
} else {
StrCol = StrCol + User.UNIT
Values = Values + ConvertStr(user.getUnit())
}
}
if (user.getRegion() != null &&!user.getRegion().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.REGION
Values = Values + "," + ConvertStr(user.getRegion())
} else {
StrCol = StrCol + User.REGION
Values = Values + ConvertStr(user.getRegion())
}
}
if (user.getDepartments() != null &&!user.getDepartments().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.DEPARTMENTS
Values = Values + "," + ConvertStr(user.getDepartments())
} else {
StrCol = StrCol + User.DEPARTMENTS
Values = Values + ConvertStr(user.getDepartments())
}
}
if (user.getTdcj() >0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.TDCJ
Values = Values + "," + ConvertStr(user.getTdcj())
} else {
StrCol = StrCol + User.TDCJ
Values = Values + ConvertStr(user.getTdcj())
}
}
if (user.getTzcj() >0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.TZCJ
Values = Values + "," + ConvertStr(user.getTzcj())
} else {
StrCol = StrCol + User.TZCJ
Values = Values + ConvertStr(user.getTzcj())
}
}
if (user.getAddress() != null &&!user.getAddress().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.ADDRESS
Values = Values + "," + ConvertStr(user.getAddress())
} else {
StrCol = StrCol + User.ADDRESS
Values = Values + ConvertStr(user.getAddress())
}
}
if (user.getPostcode() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.POSTCODE
Values = Values + "," + ConvertStr(user.getPostcode())
} else {
StrCol = StrCol + User.POSTCODE
Values = Values + ConvertStr(user.getPostcode())
}
}
if (user.getBankcard() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.BANKCARD
Values = Values + "," + ConvertStr(user.getBankcard())
} else {
StrCol = StrCol + User.BANKCARD
Values = Values + ConvertStr(user.getBankcard())
}
}
return Sql.strIns + StrCol + Sql.strVal + Values + Sql.strEnd
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)