目的
如果你這樣寫(
public static void main(String ss[]) {
CtxUtil
public Object doInHibernate(Session session) throws HibernateException
// 保存stu
Student stu
stu
session
session
Student stu
session
session
return null;
}
});
}
你期望spring在執行完execute回調後
public Transaction beginTransaction() throws HibernateException {
errorIfClosed();
if ( rootSession != null ) {
// todo : should seriously consider not allowing a txn to begin from a child session
// can always route the request to the root session
log
}
Transaction result = getTransaction();
result
return result;
}
這個方法中的result是一個org
public Transaction getTransaction() throws HibernateException {
if (hibernateTransaction==null) {
log
hibernateTransaction = owner
}
return hibernateTransaction;
}
再次追蹤
public Transaction createTransaction(JDBCContext jdbcContext
throws HibernateException {
return new JDBCTransaction( jdbcContext
}
它返回了一個JDBCTransaction
在代碼
public void begin() throws HibernateException {
if (begun) {
return;
}
if (commitFailed) {
throw new TransactionException(
}
log
try {
toggleAutoCommit = nnection()
if (log
log
}
if (toggleAutoCommit) {
log
nnection()
}
} catch (SQLException e) {
log
throw new TransactionException(
}
callback = jdbcContext
begun = true;
committed = false;
rolledBack = false;
if (timeout >
jdbcContext
}
jdbcContext
}
在直接使用Hibernate時
public void commit() throws HibernateException {
if (!begun) {
throw new TransactionException(
}
log
if (!transactionContext
transactionContext
// flush
// rollback()
}
notifyLocalSynchsBeforeTransactionCompletion();
if (callback) {
jdbcContext
}
try {
commitAndResetAutoCommit();//重點代碼
log
committed = true;
if (callback) {
jdbcContext
}
notifyLocalSynchsAfterTransactionCompletion(Status
} catch (SQLException e) {
log
commitFailed = true;
if (callback) {
jdbcContext
}
notifyLocalSynchsAfterTransactionCompletion(Status
throw new TransactionException(
} finally {
closeIfRequired();
}
}
上面代碼中
private void commitAndResetAutoCommit() throws SQLException {
try {
nnection(mit();//這段不用說也能理解了
} finally {
toggleAutoCommit();//這段的作用是恢復connection的autocommit屬性為true
}
}
上述代碼的toggleAutoCommit()源代碼如下
private void toggleAutoCommit() {
try {
if (toggleAutoCommit) {
log
nnection()
}
} catch (Exception sqle) {
log
}
}
因此
但是
public static void main(String ss[]) {
CtxUtil
public Object doInHibernate(Session session) throws HibernateException
(nnection()
// 保存stu
Student stu
stu
session
session
Student stu
session
session
return null;
}
});
}
運行後
因此
public static void main(String ss[]) {
CtxUtil
public Object doInHibernate(Session session) throws HibernateException
nnection()
//保存stu
Student stu
stu
session
session
Student stu
session
session
nnection(mit();
//至於session的關閉就不用我們操心了
return null;
}
});
}
運行上述代碼
public static void main(String ss[]) {
CtxUtil
public Object doInHibernate(Session session) throws HibernateException
nnection()
// 保存stu
Student stu
stu
session
// session
Student stu
session
// session
nnection(mit();
return null;
}
});
}
運行上述代碼
public static void main(String ss[]) {
CtxUtil
public Object doInHibernate(Session session) throws HibernateException
nnection()
// 保存stu
Student stu
stu
session
// session
Student stu
stu
session
// session
nnection(mit();
return null;
}
});
}
至此再運行上述代碼
究其原因
一
二
解決這個問題很容易
public static void main(String ss[]) {
CtxUtil
public Object doInHibernate(Session session) throws HibernateException
nnection()
//保存stu
Student stu
stu
session
Student stu
session
session
nnection(mit();
return null;
}
});
}
運行上述代碼
至此
public static void main(String ss[]) {
CtxUtil
CtxUtil
public Object doInHibernate(Session session) throws HibernateException
nnection()
//保存stu
Student stu
stu
session
Student stu
stu
session
session
nnection(mit();
return null;
}
});
}
通過設置HibernateTemplate的flushMode=FLUSH_NEVER來通知spring不進行session
protected void flushIfNecessary(Session session
if (getFlushMode() == FLUSH_EAGER || (!existingTransaction && getFlushMode() != FLUSH_NEVER)) {
logger
session
}
}
至此
本例通過這麼一個雖然簡單但又繞來繞去的例子
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27857.html