1

当我使用 Spring + Hibernate 时,我得到了一些奇怪的缓存。

A行将新的一行数据插入到数据库中,年份为2012。B
行从DB中获取,发现年份是2012。C行将年份
更新为1970。D
行发现年份仍然是2012,我不明白为什么会发生这种情况? 但是如果我注释掉 B 行,D 行得到 1970,这似乎是某种缓存。或者如果在 findLock() 中,我使用 openSession() 而不是 getCurrentSession(),则 D 行也会得到 1970。abybody 可以解释这种行为吗?

试驾

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext-Test.xml")
@TransactionConfiguration(defaultRollback = true,transactionManager="transactionManager")
@Transactional
public class OperationLockDAOTest {

    @Autowired
    private OperationLockDAO operationLockDAO;
    @Autowired
    private APIOperationDAO apiOperationDAO;

    private APIOperation operation;

    @Before
    public void setup() {
        operation = apiOperationDAO.findOperationById(Constants.OP_CREATE_SUBSCRIBER);  
    }

    @Test
    public void testAddNewLockAndReleaseLock() throws DBException{
        String userKey = "testUserKey1" + Math.random();

        boolean bGetLock = operationLockDAO.lockOperationByUser(userKey, operation);//line A, insert 2012
        List<OperationLock> locks1 = operationLockDAO.findLock(userKey, operation);//line B
        OperationLock lock1 = locks1.get(0);
        Calendar cb = Calendar.getInstance();
        cb.setTime(lock1.getModifytime());//get 2012


        operationLockDAO.unlockOperationByUser(userKey, operation);//line C, update to 1970

        List<OperationLock> locks2 = operationLockDAO.findLock(userKey, operation);//line D
        OperationLock lock2 = locks2.get(0);

        Calendar cr = Calendar.getInstance();
        cr.setTime(lock2.getModifytime());
        int crYear = cr.get(Calendar.YEAR);// still get 2012

        assertTrue(crYear == Constants.LongBeforeYear);//assert time stamp of lock is set to 1970 after release 
    }
}

OperationLockDAOImpl.java

@Repository("operationLockDAOImpl")
public class OperationLockDAOImpl implements OperationLockDAO{

    protected static final Logger logger = LoggerFactory.getLogger(OperationLockDAOImpl.class);

    /**
     * return true - this operation is not locked by another thread, lock behavior is successful this time
     *        false - this operation is locked by another thread, lock behavior failed this time
     */
    @SuppressWarnings("unchecked")
    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public boolean lockOperationByUser(String userKey, APIOperation lockoperation) {

        String selecSsql = "select * from operation_lock where userKey=:userKey and lockoperationId=:lockoperationId";
        Session session = this.factory.getCurrentSession();
        Query selectQuery = session.createSQLQuery(selecSsql).addEntity(OperationLock.class);
        selectQuery.setString("userKey", userKey);
        selectQuery.setLong("lockoperationId", lockoperation.getId());
        List<OperationLock> operationLockList = selectQuery.list();

        if(operationLockList == null || operationLockList.size() == 0){//no record for this userKey and operation, insert one anyway
            String insertSql = "insert into operation_lock(`userKey`,`lockoperationId`,`modifytime`) values (:userKey, :lockoperationId, :updateTime)";
            Query insertQuery = session.createSQLQuery(insertSql);
            insertQuery.setString("userKey", userKey);
            insertQuery.setLong("lockoperationId", lockoperation.getId());
            insertQuery.setTimestamp("updateTime", new Date());
            insertQuery.executeUpdate();
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void unlockOperationByUser(String userKey, APIOperation lockoperation) {

        Date currentTime = new Date();
        Calendar time = Calendar.getInstance();
        time.setTime(currentTime);
        time.set(Calendar.YEAR, Constants.LongBeforeYear);//it's long before
        String sql = "update operation_lock set modifytime=:updatetime where userKey=:userKey and lockoperationId=:lockoperationId";
        Session session = this.factory.getCurrentSession();
        Query query = session.createSQLQuery(sql);
        query.setTimestamp("updatetime", time.getTime());
        query.setString("userKey", userKey);
        query.setLong("lockoperationId", lockoperation.getId());
        query.executeUpdate();
    }

    @SuppressWarnings("unchecked")
    @Transactional(isolation = Isolation.SERIALIZABLE)
    @Override
    public List<OperationLock> findLock(String userKey, APIOperation lockoperation) {

        String sql = "select * from operation_lock where userKey=:userKey and lockoperationId=:lockoperationId";
//      Session session = this.factory.openSession();
        Session session = this.factory.getCurrentSession();
        Query query = session.createSQLQuery(sql).addEntity(OperationLock.class);
        query.setString("userKey", userKey);
        query.setLong("lockoperationId", lockoperation.getId());

        List<OperationLock> result =  query.list();
//      session.close();
        return result;

    }
}
4

1 回答 1

1

我认为问题在于 Hibernate(和 JPA)并不是真正打算作为本机 SQL 的接口,尤其是 SQL 更新查询。Hibernate 将维护一个会话级缓存。通常它知道实体何时更新,以便条目不会在会话缓存中过时(至少不在同一个线程中)。但是,由于您使用 SQL 更新查询更新实体,Hibernate 不知道您正在更改其缓存中的实体,因此它不能使其无效。

总之,Hibernate 缓存不适用于本机 SQL 更新查询。要使其正常工作,您必须在其自己的独立事务中维护每个操作(从测试类中删除 @Transactional),但这最终会导致性能问题。在 Hibernate 中修改实体的首选方法是......

Entity foo = session.get(Entity.class,entityId);
foo.x = y;
Entity fooModified = session.get(Entity.class,entityId);
//fooModified.x == y
于 2012-11-13T22:03:26.137 回答