項目裡面用的是jdbc 操作類 然後自己封裝了一些操作增刪改查 包括數據庫和類的映射
有問題的是怎麼去做 用spring 管理jdbc的事務 (或者自己書寫!)
1創建 jdbc操作類 IcyDataTemplate
public class IcyDataTemplate implements DataSource {
DataSource dataSource;
public PrintWriter getLogWriter() throws SQLException {
return dataSource.getLogWriter();
}
public void setLogWriter(PrintWriter out) throws SQLException {
}
public void setLoginTimeout(int seconds) throws SQLException {
}
public int getLoginTimeout() throws SQLException {
return dataSource.getLoginTimeout();
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return dataSource.getParentLogger();
}
public <T> T unwrap(Class<T> iface) throws SQLException {
return dataSource.unwrap(iface);
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return dataSource.isWrapperFor(iface);
}
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public Connection getConnection(String username, String password) throws SQLException {
return dataSource.getConnection(username, password);
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
2創建IcyDataTemplateFactory 數據源管理類
import java.sql.Connection;
import java.sql.SQLException;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import customerEnter.Frame.JDBCDao.JdbcTemplate.IcyDataTemplate;
public class IcyDataTemplateFactory {
IcyDataTemplate icyDataTemplate;
public Connection getSession() {
if (TransactionSynchronizationManager.hasResource(this)) {
return getCurrentSession();
} else {
return openSession();
}
}
private Connection openSession() {
Connection connection = null;
try {
connection = getIcyDataTemplate().getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
TransactionSynchronization transactionSynchronization = new IcyTransaction(this);
TransactionSynchronizationManager.registerSynchronization(transactionSynchronization);
TransactionSynchronizationManager.bindResource(this, connection);
System.out.println("打開新的connection");
return connection;
}
private Connection getCurrentSession() {
System.out.println("获取当前线程的 connection");
return (Connection) TransactionSynchronizationManager.getResource(this);
}
public IcyDataTemplate getIcyDataTemplate() {
return icyDataTemplate;
}
public void setIcyDataTemplate(IcyDataTemplate icyDataTemplate) {
this.icyDataTemplate = icyDataTemplate;
}
public Connection getConnection() throws SQLException {
return icyDataTemplate.getConnection();
}
}
3 創建IcyTransaction
import java.sql.Connection;
import java.sql.SQLException;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import customerEnter.Frame.Error.FramErrorProcessing;
import customerEnter.Frame.Utils.Log4j;
/**
*
* 类描述: 事物處理
*
* 类名称:MyTransactionSynchronizationAdapter
*
* 创建人:dy
*
* 创建时间:2017-7-27 下午3:32:06
*
*/
public class IcyTransaction extends TransactionSynchronizationAdapter {
private IcyDataTemplateFactory icyDataTemplateFactory;
public IcyTransaction(IcyDataTemplateFactory icyDataTemplateFactory) {
this.icyDataTemplateFactory = icyDataTemplateFactory;
}
public void suspend() {
}
public void resume() {
}
public void beforeCompletion() {
}
@Override
public void beforeCommit(boolean Isex) {
if (!Isex) {
Connection connection = (Connection) TransactionSynchronizationManager.getResource(icyDataTemplateFactory);
System.out.println("開啟事物 " + connection);
}
}
@Override
public void afterCompletion(int status) {
Connection connection = (Connection) TransactionSynchronizationManager.getResource(icyDataTemplateFactory);
if (STATUS_COMMITTED == status) {
try {
connection.commit();
System.out.println("提交 ");
} catch (SQLException e) {
System.out.println("提交失敗 ");
System.out.println(e.getMessage());
}
} else {
try {
connection.rollback();
System.out.println("回滾 ");
} catch (SQLException e) {
System.out.println("回滾失敗 ");
System.out.println(e.getMessage());
}
}
try {
closeConn(connection);
} catch (FramErrorProcessing e) {
Log4j.setLog4j(getClass());
Log4j.Log4JOutError("報錯 關閉鏈接" + e.getMessage());
}
}
void closeConn(Connection connection) throws FramErrorProcessing {
try {
connection.close();
} catch (SQLException e) {
throw new FramErrorProcessing(e);
}
}
}
然後分別在spring文件裡注入 icyDataTemplate和icyDataTemplateFactory
然後用spring獲取 icyDataTemplateFactory.getSession就是有事務的Connection
但是要在spring 文件里配置好註解方式的事務 然後在server 寫上事務註解 就可以了