use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class TestingServiceBean method executeSelectSql.
@Override
@Transactional(timeout = 2)
public String executeSelectSql(String sql) {
checkTestMode();
log.info("started: " + sql);
EntityManager em = persistence.getEntityManager();
Query query = em.createNativeQuery(sql);
query.getResultList();
log.info("finished: " + sql);
return "Done";
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class TestingServiceBean method executeUpdateSql.
@Override
@Transactional(timeout = 2)
public String executeUpdateSql(String sql) {
checkTestMode();
log.info("started: " + sql);
EntityManager em = persistence.getEntityManager();
Query query = em.createNativeQuery(sql);
query.executeUpdate();
log.info("finished: " + sql);
return "Done";
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class UniqueNumbers method checkSequenceExists.
protected void checkSequenceExists(String domain) {
String seqName = getSequenceName(domain);
if (containsSequence(seqName))
return;
// Create sequence in separate transaction because it's name is cached and we want to be sure it is created
// regardless of possible errors in the invoking code
Transaction tx = persistence.createTransaction(getDataStore(domain));
try {
lock.readLock().unlock();
lock.writeLock().lock();
EntityManager em = persistence.getEntityManager(getDataStore(domain));
Query query = em.createNativeQuery(getSequenceSupport(domain).sequenceExistsSql(seqName));
List list = query.getResultList();
if (list.isEmpty()) {
query = em.createNativeQuery(getSequenceSupport(domain).createSequenceSql(seqName, 1, 1));
query.executeUpdate();
}
tx.commit();
existingSequences.add(seqName);
} finally {
lock.readLock().lock();
lock.writeLock().unlock();
tx.end();
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class UniqueNumbers method executeScript.
protected Object executeScript(String domain, String sqlScript) {
EntityManager em = persistence.getEntityManager(getDataStore(domain));
StrTokenizer tokenizer = new StrTokenizer(sqlScript, SequenceSupport.SQL_DELIMITER);
Object value = null;
Connection connection = em.getConnection();
while (tokenizer.hasNext()) {
String sql = tokenizer.nextToken();
try {
PreparedStatement statement = connection.prepareStatement(sql);
try {
if (statement.execute()) {
ResultSet rs = statement.getResultSet();
if (rs.next())
value = rs.getLong(1);
}
} finally {
DbUtils.closeQuietly(statement);
}
} catch (SQLException e) {
throw new IllegalStateException("Error executing SQL for getting next number", e);
}
}
return value;
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class StandardCacheLoader method updateData.
@Override
public void updateData(CacheSet cacheSet, Map<String, Object> params) throws CacheException {
if (configuration.getConfig(GlobalConfig.class).getPerformanceTestMode())
return;
Collection<Object> items = cacheSet.getItems();
List updateItems = (List) params.get("items");
if ((updateItems != null) && (updateItems.size() > 0)) {
MetaClass metaClass = metadata.getSession().getClass(metaClassName);
View view = metadata.getViewRepository().getView(metaClass, viewName);
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
for (Object item : updateItems) {
Entity entity = (Entity) item;
entity = em.find(entity.getClass(), entity.getId(), view);
items.remove(item);
if (entity != null)
items.add(entity);
}
tx.commit();
} catch (Exception e) {
throw new CacheException(e);
} finally {
tx.end();
}
} else {
log.debug("Nothing to update");
}
}
Aggregations