数据库操作基类设计4
/**
* 添加友情链接 同时处理多张表
* @return 是否添加成功
*/
public boolean addLinks(LinksInfo linksInfo) throws SQLException
{
SqlMapClient sqlMapClient = getSqlMapClient(); // 调用父类方法获取SqlMapClient
try
{
sqlMapClient.startTransaction();
// 当前连接设置为非自动提交,然后开始操作数据库
sqlMapClient.getCurrentConnection().setAutoCommit(false);
sqlMapClient.insert("LinksInfo.insert", linksInfo);
...........
if (......)
{
for (......)
{
sqlMapClient.insert("LinksInfo.insertVp", ...);
}
}
sqlMapClient.getCurrentConnection().commit(); // 当前连接提交
}
finally
{
ColumnHelper.setAutoCommit(sqlMapClient);
}
return true;
}
public static void setAutoCommit(SqlMapClient sqlMapClient) // Class ColumnHelper ...
{
try // 恢复当前连接设置为自动提交
{
sqlMapClient.getCurrentConnection().setAutoCommit(true);
}
catch (SQLException e)
{
LogManager.warn(LinksService.MODEULENAME_LINKS, "Database exception:", e);
}
try // 结束当前事务,释放会话
{
sqlMapClient.endTransaction();
}
catch (SQLException e)
{
LogManager.warn(LinksService.MODEULENAME_LINKS, "Database exception:", e);
}
}