반응형
Hibernate에서 트랜잭션의 객체를 save ()
한 다음 롤백하면 저장된 객체는 여전히 DB에 남아 있습니다. 이 문제는 update ()
또는 delete ()
메서드에서 발생하지 않고 save ()
에서만 발생하기 때문에 이상합니다.
내가 사용하는 코드는 다음과 같습니다.
DbEntity dbEntity = getDbEntity();
HibernateUtil.beginTransaction();
Session session = HibernateUtil.getCurrentSession();
session.save(dbEntity);
HibernateUtil.rollbackTransaction();
그리고 여기에 HibernateUtil 클래스가 있습니다 (관련된 함수들, 저는 getSessionFactory ()
메소드가 잘 작동 함을 보장합니다-인터셉터 핸들러가 있지만 지금은 중요하지 않습니다) :
private static final ThreadLocal<Session> threadSession = new ThreadLocal<Session>();
private static final ThreadLocal<Transaction> threadTransaction = new ThreadLocal<Transaction>();
/**
* Retrieves the current Session local to the thread.
* <p/>
* If no Session is open, opens a new Session for the running thread.
*
* @return Session
*/
public static Session getCurrentSession()
throws HibernateException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
log.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
log.debug("Using interceptor: " + getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
return s;
}
/**
* Start a new database transaction.
*/
public static void beginTransaction()
throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
log.debug("Starting new database transaction in this thread.");
tx = getCurrentSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
}
/**
* Rollback the database transaction.
*/
public static void rollbackTransaction()
throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
log.debug("Tyring to rollback database transaction of this thread.");
tx.rollback();
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
} finally {
closeSession();
}
}
감사
해결 방법
데이터베이스가 롤백을 지원하는지 확인하십시오. 즉, MyISAM이 아닌 InnoDB 테이블을 사용 중인지 확인하십시오 (트랜잭션 테이블과 비 트랜잭션 테이블을 혼합 할 수 있지만 대부분의 경우 모든 테이블이 InnoDB가되기를 원합니다).
참조 페이지 https://stackoverflow.com/questions/2605594
반응형
'MySql' 카테고리의 다른 글
MySQL PHP의 Hibernate와 비슷한 것이 있습니까? (0) | 2020.11.30 |
---|---|
MySQL 오류 : stdClass 클래스의 개체를 문자열로 변환 할 수 없습니다. (0) | 2020.11.30 |
MySQL 날짜를 기준으로 테이블에서 데이터 선택 (0) | 2020.11.30 |
MySQL How can a not null constraint be dropped? (0) | 2020.11.30 |
MySQL Specifying a variable name in QUERY WHERE clause in JDBC (0) | 2020.11.30 |
댓글