Search in sources :

Example 1 with OptimisticLockException

use of org.seasar.doma.jdbc.OptimisticLockException in project doma-spring-boot by domaframework.

the class DomaPersistenceExceptionTranslator method translateExceptionIfPossible.

@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
    if (!(ex instanceof JdbcException)) {
        // Fallback to other translators if not JdbcException
        return null;
    }
    if (ex instanceof OptimisticLockException) {
        return new OptimisticLockingFailureException(ex.getMessage(), ex);
    } else if (ex instanceof UniqueConstraintException) {
        return new DuplicateKeyException(ex.getMessage(), ex);
    } else if (ex instanceof NonUniqueResultException || ex instanceof NonSingleColumnException) {
        return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
    } else if (ex instanceof NoResultException) {
        return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
    } else if (ex instanceof UnknownColumnException || ex instanceof ResultMappingException) {
        return new TypeMismatchDataAccessException(ex.getMessage(), ex);
    }
    if (ex.getCause() instanceof SQLException) {
        SQLException e = (SQLException) ex.getCause();
        String sql = null;
        if (ex instanceof SqlExecutionException) {
            sql = ((SqlExecutionException) ex).getRawSql();
        }
        DataAccessException dae = translator.translate(ex.getMessage(), sql, e);
        return (dae != null ? dae : new UncategorizedSQLException(ex.getMessage(), sql, e));
    }
    return new UncategorizedDataAccessException(ex.getMessage(), ex) {
    };
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) NonSingleColumnException(org.seasar.doma.jdbc.NonSingleColumnException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) NoResultException(org.seasar.doma.jdbc.NoResultException) ResultMappingException(org.seasar.doma.jdbc.ResultMappingException) JdbcException(org.seasar.doma.jdbc.JdbcException) TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) UncategorizedDataAccessException(org.springframework.dao.UncategorizedDataAccessException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) UnknownColumnException(org.seasar.doma.jdbc.UnknownColumnException) SqlExecutionException(org.seasar.doma.jdbc.SqlExecutionException) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) UniqueConstraintException(org.seasar.doma.jdbc.UniqueConstraintException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException) UncategorizedDataAccessException(org.springframework.dao.UncategorizedDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 2 with OptimisticLockException

use of org.seasar.doma.jdbc.OptimisticLockException in project doma by domaframework.

the class AutoBatchUpdateTest method testOptimisticLockException.

@Test
public void testOptimisticLockException(Config config) throws Exception {
    DepartmentDao dao = new DepartmentDaoImpl(config);
    Department department1 = dao.selectById(1);
    department1.setDepartmentName("hoge");
    Department department2 = dao.selectById(2);
    department2.setDepartmentName("foo");
    Department department3 = dao.selectById(1);
    department3.setDepartmentName("bar");
    dao.update(department1);
    try {
        dao.update(Arrays.asList(department2, department3));
        fail();
    } catch (OptimisticLockException expected) {
    }
}
Also used : DepartmentDaoImpl(org.seasar.doma.it.dao.DepartmentDaoImpl) CompKeyDepartmentDaoImpl(org.seasar.doma.it.dao.CompKeyDepartmentDaoImpl) Department(org.seasar.doma.it.entity.Department) CompKeyDepartment(org.seasar.doma.it.entity.CompKeyDepartment) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) DepartmentDao(org.seasar.doma.it.dao.DepartmentDao) CompKeyDepartmentDao(org.seasar.doma.it.dao.CompKeyDepartmentDao) Test(org.junit.jupiter.api.Test)

Example 3 with OptimisticLockException

use of org.seasar.doma.jdbc.OptimisticLockException in project doma by domaframework.

the class AutoDeleteTest method testOptimisticLockException.

@Test
public void testOptimisticLockException(Config config) throws Exception {
    EmployeeDao dao = new EmployeeDaoImpl(config);
    Employee employee1 = dao.selectById(Integer.valueOf(1));
    employee1.setEmployeeName("hoge");
    Employee employee2 = dao.selectById(Integer.valueOf(1));
    employee2.setEmployeeName("foo");
    dao.delete(employee1);
    try {
        dao.delete(employee2);
        fail();
    } catch (OptimisticLockException expected) {
    }
}
Also used : EmployeeDao(org.seasar.doma.it.dao.EmployeeDao) CompKeyEmployeeDao(org.seasar.doma.it.dao.CompKeyEmployeeDao) CompKeyEmployee(org.seasar.doma.it.entity.CompKeyEmployee) Employee(org.seasar.doma.it.entity.Employee) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) EmployeeDaoImpl(org.seasar.doma.it.dao.EmployeeDaoImpl) CompKeyEmployeeDaoImpl(org.seasar.doma.it.dao.CompKeyEmployeeDaoImpl) Test(org.junit.jupiter.api.Test)

Example 4 with OptimisticLockException

use of org.seasar.doma.jdbc.OptimisticLockException in project doma by domaframework.

the class AutoUpdateTest method testTenantId.

@Test
public void testTenantId(Config config) throws Exception {
    SalesmanDao dao = new SalesmanDaoImpl(config);
    Salesman salesman = dao.selectById(1);
    Integer tenantId = salesman.departmentId;
    salesman.departmentId = -1;
    try {
        dao.update(salesman);
        fail();
    } catch (OptimisticLockException expected) {
    }
    salesman.departmentId = tenantId;
    dao.update(salesman);
}
Also used : Salesman(org.seasar.doma.it.entity.Salesman) SalesmanDao(org.seasar.doma.it.dao.SalesmanDao) SalesmanDaoImpl(org.seasar.doma.it.dao.SalesmanDaoImpl) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) Test(org.junit.jupiter.api.Test)

Example 5 with OptimisticLockException

use of org.seasar.doma.jdbc.OptimisticLockException in project doma by domaframework.

the class AutoBatchDeleteTest method testTenantId.

@Test
public void testTenantId(Config config) throws Exception {
    SalesmanDao dao = new SalesmanDaoImpl(config);
    Salesman salesman = dao.selectById(1);
    Integer tenantId = salesman.departmentId;
    salesman.departmentId = -1;
    try {
        dao.delete(Arrays.asList(salesman));
        fail();
    } catch (OptimisticLockException expected) {
    }
    salesman.departmentId = tenantId;
    dao.delete(Arrays.asList(salesman));
}
Also used : Salesman(org.seasar.doma.it.entity.Salesman) SalesmanDao(org.seasar.doma.it.dao.SalesmanDao) SalesmanDaoImpl(org.seasar.doma.it.dao.SalesmanDaoImpl) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) Test(org.junit.jupiter.api.Test)

Aggregations

OptimisticLockException (org.seasar.doma.jdbc.OptimisticLockException)13 Test (org.junit.jupiter.api.Test)12 SalesmanDao (org.seasar.doma.it.dao.SalesmanDao)4 SalesmanDaoImpl (org.seasar.doma.it.dao.SalesmanDaoImpl)4 Salesman (org.seasar.doma.it.entity.Salesman)4 Emp (example.entity.Emp)3 example.entity._Emp (example.entity._Emp)3 DepartmentDao (org.seasar.doma.it.dao.DepartmentDao)3 DepartmentDaoImpl (org.seasar.doma.it.dao.DepartmentDaoImpl)3 Department (org.seasar.doma.it.entity.Department)3 CompKeyDepartmentDao (org.seasar.doma.it.dao.CompKeyDepartmentDao)2 CompKeyDepartmentDaoImpl (org.seasar.doma.it.dao.CompKeyDepartmentDaoImpl)2 CompKeyEmployeeDao (org.seasar.doma.it.dao.CompKeyEmployeeDao)2 CompKeyEmployeeDaoImpl (org.seasar.doma.it.dao.CompKeyEmployeeDaoImpl)2 EmployeeDao (org.seasar.doma.it.dao.EmployeeDao)2 EmployeeDaoImpl (org.seasar.doma.it.dao.EmployeeDaoImpl)2 CompKeyDepartment (org.seasar.doma.it.entity.CompKeyDepartment)2 CompKeyEmployee (org.seasar.doma.it.entity.CompKeyEmployee)2 Employee (org.seasar.doma.it.entity.Employee)2 SQLException (java.sql.SQLException)1