当前页面: 开发资料首页 → JSP 专题 → 数据库的事物问题
数据库的事物问题
摘要: 数据库的事物问题
在JAVA里如何实现数据库的事务操作呢?
我向两个表里同时插东西
想做成一个事务来实现
这样好防止异常的产生
例如
表A记录用户登陆信息,表B记录用户的详细信息
他们之间有共同的用户ID
需要同时进行插入操作
帮顶
的源码
public abstract class TransactionTagSupport extends TagSupport
implements TryCatchFinally {
//*********************************************************************
// Private constants
private static final String TRANSACTION_READ_COMMITTED
= "read_committed";
private static final String TRANSACTION_READ_UNCOMMITTED
= "read_uncommitted";
private static final String TRANSACTION_REPEATABLE_READ
= "repeatable_read";
private static final String TRANSACTION_SERIALIZABLE
= "serializable";
//*********************************************************************
// Protected state
protected Object rawDataSource;
protected boolean dataSourceSpecified;
//*********************************************************************
// Private state
private Connection conn;
private int isolation;
private int origIsolation;
//*********************************************************************
// Constructor and initialization
public TransactionTagSupport() {
super();
init();
}
private void init() {
conn = null;
dataSourceSpecified = false;
rawDataSource = null;
isolation = Connection.TRANSACTION_NONE;
}
//*********************************************************************
// Tag logic
/**
* Prepares for execution by setting the initial state, such as
* getting the Connection
and preparing it for
* the transaction.
*/
public int doStartTag() throws JspException {
if ((rawDataSource == null) && dataSourceSpecified) {
throw new JspException(
Resources.getMessage("SQL_DATASOURCE_NULL"));
}
DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource,
pageContext);
try {
conn = dataSource.getConnection();
origIsolation = conn.getTransactionIsolation();
if (origIsolation == Connection.TRANSACTION_NONE) {
throw new JspTagException(
Resources.getMessage("TRANSACTION_NO_SUPPORT"));
}
if ((isolation != Connection.TRANSACTION_NONE)
&& (isolation != origIsolation)) {
conn.setTransactionIsolation(isolation);
}
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new JspTagException(
Resources.getMessage("ERROR_GET_CONNECTION",
e.getMessage()));
}
return EVAL_BODY_INCLUDE;
}
/**
* Commits the transaction.
*/
public int doEndTag() throws JspException {
try {
conn.commit();
} catch (SQLException e) {
throw new JspTagException(
Resources.getMessage("TRANSACTION_COMMIT_ERROR",
e.getMessage()));
}
return EVAL_PAGE;
}
/**
* Rollbacks the transaction and rethrows the Throwable.
*/
public void doCatch(Throwable t) throws Throwable {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e) {
// Ignore to not hide orignal exception
}
}
throw t;
}
/**
* Restores the Connection
to its initial state and
* closes it.
*/
public void doFinally() {
if (conn != null) {
try {
if ((isolation != Connection.TRANSACTION_NONE)
&& (isolation != origIsolation)) {
conn.setTransactionIsolation(origIsolation);
}
conn.setAutoCommit(true);
conn.close();
} catch (SQLException e) {
// Not much we can do
}
}
conn = null;
}
// Releases any resources we may have (or inherit)
public void release() {
init();
}
//*********************************************************************
// Public utility methods
/**
* Setter method for the transaction isolation level.
*/
public void setIsolation(String iso) throws JspTagException {
if (TRANSACTION_READ_COMMITTED.equals(iso)) {
isolation = Connection.TRANSACTION_READ_COMMITTED;
} else if (TRANSACTION_READ_UNCOMMITTED.equals(iso)) {
isolation = Connection.TRANSACTION_READ_UNCOMMITTED;
} else if (TRANSACTION_REPEATABLE_READ.equals(iso)) {
isolation = Connection.TRANSACTION_REPEATABLE_READ;
} else if (TRANSACTION_SERIALIZABLE.equals(iso)) {
isolation = Connection.TRANSACTION_SERIALIZABLE;
} else {
throw new JspTagException(
Resources.getMessage("TRANSACTION_INVALID_ISOLATION"));
}
}
/**
* Called by nested parameter elements to get a reference to
* the Connection.
*/
public Connection getSharedConnection() {
return conn;
}
}
问题已经由自己解决了
谢谢楼上的兄弟了