Skip to content

Commit

Permalink
KAA-600: Update CTL dao implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Khanenko committed Nov 11, 2015
1 parent b98e402 commit de63ebb
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 27 deletions.
@@ -1,4 +1,7 @@
package org.kaaproject.kaa.server.common.dao.impl;

import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema;

public interface CTLSchemaDao<T> extends SqlDao<T> {
CTLSchema findByFqnAndVersion(String fqn, Integer version);
}
Expand Up @@ -16,6 +16,7 @@
package org.kaaproject.kaa.server.common.dao.impl.sql;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
Expand Down Expand Up @@ -127,6 +128,15 @@ protected T findOneByCriterion(Criterion criterion) {
return (T) criteria.uniqueResult();
}

protected T findOneByCriterionWithLock(Criterion criterion, LockMode lockMode) {
String className = getSimpleClassName();
LOG.trace("Searching {} entity by criterion [{}] ", className, criterion);
Criteria criteria = getCriteria();
criteria.setLockMode(lockMode);
criteria.add(criterion);
return (T) criteria.uniqueResult();
}

protected T findOneByCriterionWithAlias(String path, String alias, Criterion criterion) {
String className = getSimpleClassName();
LOG.trace("Searching {} entity by criterion [{}] ", className, criterion);
Expand Down
@@ -1,27 +1,50 @@
package org.kaaproject.kaa.server.common.dao.impl.sql;

import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.kaaproject.kaa.server.common.dao.impl.CTLSchemaDao;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import static org.apache.commons.lang.StringUtils.isNotBlank;

@Repository
public class HibernateCTLSchemaDao extends HibernateAbstractDao<CTLSchema> implements CTLSchemaDao<CTLSchema> {

LockOptions lockOptions = new LockOptions(LockMode.PESSIMISTIC_WRITE);
private static final Logger LOG = LoggerFactory.getLogger(HibernateCTLSchemaDao.class);

@Override
public CTLSchema save(CTLSchema o) {
Session session = getSession();
CTLSchema t = (CTLSchema) session.merge(o);
session.flush();
return t;
if (o.getTenant() == null) {
CTLSchema ctlSchema = findByFqnAndVersion(o.getFqn(), o.getVersion());
if (ctlSchema != null) {
throw new RuntimeException("Can't save system ctl schema with same fqn and version.");
}
}
return super.save(o);
}

@Override
protected Class<CTLSchema> getEntityClass() {
return CTLSchema.class;
}

@Override
public CTLSchema findByFqnAndVersion(String fqn, Integer version) {
CTLSchema ctlSchema = null;
LOG.debug("Searching ctl schema by fqn [{}] and version [{}]", fqn, version);
if (isNotBlank(fqn) && version != null) {
ctlSchema = findOneByCriterionWithLock(Restrictions.and(
Restrictions.eq("version", version),
Restrictions.eq("fqn", fqn)), LockMode.PESSIMISTIC_WRITE);
}
if (LOG.isTraceEnabled()) {
LOG.trace("[{},{}] Search result: {}.", fqn, version, ctlSchema);
} else {
LOG.debug("[{},{}] Search result: {}.", fqn, version, ctlSchema != null);
}
return ctlSchema;
}
}
@@ -0,0 +1,20 @@
package org.kaaproject.kaa.server.common.dao.service;


import org.kaaproject.kaa.server.common.dao.impl.CTLSchemaDao;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CTLSchemaServiceImpl {

@Autowired
private CTLSchemaDao<CTLSchema> ctlSchemaDao;

public CTLSchema save(CTLSchema ctlSchema) {
return ctlSchemaDao.save(ctlSchema);
}
}
@@ -1,66 +1,90 @@
package org.kaaproject.kaa.server.common.dao.impl.sql;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kaaproject.kaa.common.dto.TenantDto;
import org.kaaproject.kaa.server.common.dao.UserService;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchemaScope;
import org.kaaproject.kaa.server.common.dao.model.sql.Tenant;
import org.kaaproject.kaa.server.common.dao.service.CTLSchemaServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Future;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/common-dao-test-context.xml")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
public class HibernateCTLSchemaDaoTest extends HibernateAbstractTest {

private static final Logger LOG = LoggerFactory.getLogger(HibernateCTLSchemaDaoTest.class);
public static final String SUPER_TENANT = "SuperTenant";
@Autowired
private CTLSchemaServiceImpl ctlSchemaService;
@Autowired
private UserService userService;

private ExecutorService executorService = Executors.newFixedThreadPool(10);
private Tenant tenant;
private TenantDto tenant;

@Before
@Rollback(false)
public void go() {
if (tenant == null) {
tenant = generateTenant();
tenant = userService.findTenantByName(SUPER_TENANT);
if (tenant == null) {
TenantDto tn = new TenantDto();
tn.setName(SUPER_TENANT);
tenant = userService.saveTenant(tn);
}
}
}

@After
public void afterTest() throws InterruptedException {
LOG.debug("If sdsfsddf");
}

@Test
@Rollback(false)
public void saveCTLSchema() throws InterruptedException {
ctlSchemaDao.save(generateCTLSchema(tenant));
ctlSchemaDao.save(generateCTLSchema(generateTenant()));
ctlSchemaService.save(generateCTLSchema(new Tenant(tenant)));
ctlSchemaService.save(generateCTLSchema(null));
}

@Test
@Rollback(false)
public void multiThreadCTLSchemaSaveTest() throws InterruptedException {
public void multiThreadCTLSchemaSaveTest() throws InterruptedException, ExecutionException {
List<Future<CTLSchema>> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
executorService.submit(new Runnable() {
final int x = i;
list.add(executorService.submit(new Callable<CTLSchema>() {
@Override
public void run() {
ctlSchemaDao.save(generateCTLSchema(generateTenant()));
public CTLSchema call() {
CTLSchema sch = null;
try {
if (x % 2 == 0) {
sch = ctlSchemaService.save(generateCTLSchema(new Tenant(tenant)));
} else {
sch = ctlSchemaService.save(generateCTLSchema(null));
}
} catch (Throwable t) {
LOG.warn("Catch exception {}", t.getCause(), t);
}
return sch;
}
});
}));
}
for (Future<CTLSchema> f : list) {
LOG.debug("id {}", f.get().getId());
}
}

Expand Down
@@ -0,0 +1,6 @@
package org.kaaproject.kaa.server.common.dao.service;



public class CTLSchemaServiceImplTest {
}
Expand Up @@ -47,7 +47,7 @@

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/kaa"/>
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/kaa"/>
<property name="username" value="postgres"/>
<property name="password" value="admin"/>
</bean>
Expand Down

0 comments on commit de63ebb

Please sign in to comment.