我想要实现的是,我想在select语句中执行no lock查询。但根据this回答,直接JPA实现是不可能实现的。我也从nolock
和READ_UNCOMMITTED
相同的搜索中了解到。有没有办法通过修改我的下面的代码来实现这一点(没有锁,READ_UNCOMMITTED)。或者我应该使用本机查询并指定WITH(NOLOCK)
我曾尝试过
entityManager.createQuery(query).setLockMode(LockModeType.NONE).getResultList();
但它也没有解决我的问题。
我使用以下代码从表中获取数据。这段代码很好,没有nolock。
String query = "FROM Employee WHERE empId=:empId AND empStatus='failed'";
从db
获取数据public Object getListFromQuery(String query) throws Exception {
Object resultObject = null;
List<Object> queryResultList = null;
EntityManager entityManager = null;
try {
entityManager = entityManagerFactory.createEntityManager();
queryResultList = entityManager.createQuery(query).getResultList();
resultObject = (Object) queryResultList;
} catch (Exception ex) {
LOGGER.error("Exception : DatabaseManager :executeQueryGetList ",ex);
throw ex;
} finally {
entityManager.close();
}
return resultObject;
}
数据库配置
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
throws NamingException {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource());
factoryBean
.setPackagesToScan(new String[] { "com.test.middleware.entity" });
factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
factoryBean.setJpaProperties(jpaProperties());
factoryBean.setPersistenceUnitName("test_unit");
return factoryBean;
}