配置文件:
DriverClass = com.mysql.jdbc.Driver
JdbcUrl = jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8
User = root
Password = 123456
MaxPoolSize = 20
MinPoolSize = 2
InitialPoolSize = 5
MaxStatements = 30
MaxIdleTime =100
工程目录结构
package com.jta.c3p0;
import java.sql.Connection;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Mysql {
private ComboPooledDataSource cpds;
private static C3P0Mysql c3P0Properties;
static{
c3P0Properties = new C3P0Mysql();
}
public C3P0Mysql() {
try {
cpds = new ComboPooledDataSource();
//加载配置文件
Properties props = new Properties();
props.load(C3P0Mysql.class.getClassLoader().getResourceAsStream("config.properties"));
cpds.setDriverClass(props.getProperty("DriverClass"));
cpds.setJdbcUrl(props.getProperty("JdbcUrl"));
cpds.setUser(props.getProperty("User"));
cpds.setPassword(props.getProperty("Password"));
cpds.setMaxPoolSize(Integer.parseInt(props.getProperty("MaxPoolSize")));
cpds.setMinPoolSize(Integer.parseInt(props.getProperty("MinPoolSize")));
cpds.setInitialPoolSize(Integer.parseInt(props.getProperty("InitialPoolSize")));
cpds.setMaxStatements(Integer.parseInt(props.getProperty("MaxStatements")));
cpds.setMaxIdleTime(Integer.parseInt(props.getProperty("MaxIdleTime")));
} catch (Exception e) {
e.printStackTrace();
}
}
public static C3P0Mysql getInstance(){
return c3P0Properties;
}
public Connection getConnection(){
Connection conn = null;
try {
conn = cpds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection connection = C3P0Mysql.c3P0Properties.getConnection();
System.out.println("已经连接成功");
}
}
package com.jta;
import com.jta.c3p0.C3P0Mysql;
import com.mchange.v2.c3p0.impl.C3P0PooledConnection;
import com.mchange.v2.c3p0.impl.NewProxyConnection;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class JTA_MySQL {
public static void main(String[] args) {
XADataSource xaDs1 = JTA_MySQL.getDataSource(
"jdbc:mysql://localhost:3306/demo", "root",
"123456");
XAConnection xaCon1 = null;
XAResource xaRes1 = null;
Connection conn1 = null;
Statement stmt1 = null;
XADataSource xaDs2 = JTA_MySQL.getDataSource(
"jdbc:mysql://127.0.0.1:3306/test", "root",
"123456");
XAConnection xaCon2 = null;
XAResource xaRes2 = null;
Connection conn2 = null;
Statement stmt2 = null;
int ret1 = 0;
int ret2 = 0;
Xid xid1 = new MyXid(100, new byte[] { 100 }, new byte[] { 0x02 });
Xid xid2 = new MyXid(100, new byte[] { 0x01 }, new byte[] { 0x03 });
try {
xaCon1 = getXAConnetion(xaDs1);
conn1 = getConnection(xaCon1);
stmt1 = conn1.createStatement();
xaRes1 = xaCon1.getXAResource();
xaCon2 = getXAConnetion(xaDs2);
conn2 = getConnection(xaCon2);
stmt2 = conn2.createStatement();
xaRes2 = xaCon2.getXAResource();
xaRes1.start(xid1, XAResource.TMNOFLAGS);
stmt1.execute("delete from person where id=1");
xaRes1.end(xid1, XAResource.TMSUCCESS);
xaRes2.start(xid2, XAResource.TMNOFLAGS);
stmt2.execute("insert into person select 1, 'zhang'");
xaRes2.end(xid2, XAResource.TMSUCCESS);
ret1 = xaRes1.prepare(xid1);
ret2 = xaRes2.prepare(xid2);
if (XAResource.XA_OK == ret1 && XAResource.XA_OK == ret2) {
xaRes1.commit(xid1, false);
xaRes2.commit(xid2, false);
System.out.println("提交分布式事务");
}else{
xaRes1.rollback(xid1);
xaRes2.rollback(xid2);
System.out.println("回退分布式事务");
}
}catch (Exception e){
e.printStackTrace();
}
}
private static Connection getConnection(XAConnection xaCon1) {
Connection conn = null;
try {
conn = xaCon1.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
private static XAConnection getXAConnetion(XADataSource dataSource) {
XAConnection XAConn = null;
try {
XAConn = dataSource.getXAConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return XAConn;
}
private static XADataSource getDataSource(String url, String user, String password) {
MysqlXADataSource dataSource = new MysqlXADataSource();
dataSource.setUrl(url);
dataSource.setUser(user);
dataSource.setPassword(password);
return dataSource;
}
}
package com.jta;
import javax.transaction.xa.Xid;
public class MyXid implements Xid {
private int formatId;
private byte[] globalTid;
private byte[] branchQ;
public MyXid(int formatId, byte[] globalTid, byte[] branchQ) {
this.formatId = formatId;
this.globalTid = globalTid;
this.branchQ = branchQ;
}
@Override
public int getFormatId() {
return formatId;
}
@Override
public byte[] getGlobalTransactionId() {
return globalTid;
}
@Override
public byte[] getBranchQualifier() {
return branchQ;
}
}