Non transient exception (#593)
* non transient data access exception examples * change to derby db * change to in memory derby db * delete failed test * fix invalidresource test * fix cleanupfailure test * more exception examples
This commit is contained in:
parent
8a104053d6
commit
2817953d0b
|
@ -0,0 +1,75 @@
|
|||
package org.baeldung.ex.nontransientexception.cause;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-mysql-incorrect.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
public class Cause5NonTransientConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public Cause5NonTransientConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalSessionFactoryBean sessionFactory() {
|
||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||
sessionFactory.setDataSource(restDataSource());
|
||||
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource restDataSource() {
|
||||
final BasicDataSource dataSource = new BasicDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HibernateTransactionManager transactionManager() {
|
||||
final HibernateTransactionManager txManager = new HibernateTransactionManager();
|
||||
txManager.setSessionFactory(sessionFactory().getObject());
|
||||
|
||||
return txManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties hibernateProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
return hibernateProperties;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
# jdbc.X
|
||||
jdbc.driverClassName=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql:3306://localhost/spring_hibernate4_exceptions?createDatabaseIfNotExist=true
|
||||
jdbc.user=tutorialuser
|
||||
jdbc.pass=tutorialmy5ql
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
|
||||
hibernate.show_sql=false
|
||||
hibernate.hbm2ddl.auto=create
|
|
@ -0,0 +1,31 @@
|
|||
package org.baeldung.ex.nontransientdataaccessexception;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||
import org.baeldung.ex.nontransientexception.cause.Cause5NonTransientConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.IFooService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause5NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class CannotGetJdbcConnectionExceptionTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource restDataSource;
|
||||
|
||||
@Test(expected = CannotGetJdbcConnectionException.class)
|
||||
public void whenJdbcUrlIncorrect_thenCannotGetJdbcConnectionException() {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||
jdbcTemplate.execute("select * from foo");
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package org.baeldung.ex.nontransientdataaccessexception;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.IFooService;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.CleanupFailureDataAccessException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class CleanupFailureExceptionTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CleanupFailureExceptionTest.class.getName());
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@Autowired
|
||||
private IFooService fooService;
|
||||
|
||||
@Test
|
||||
public void whenCleanupAfterSaving_thenCleanupException() {
|
||||
try {
|
||||
final Foo fooEntity = new Foo("foo");
|
||||
fooService.create(fooEntity);
|
||||
} finally {
|
||||
try {
|
||||
sessionFactory.close();
|
||||
} catch (final CleanupFailureDataAccessException exc) {
|
||||
LOG.log(Level.SEVERE, exc.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package org.baeldung.ex.nontransientdataaccessexception;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.IFooService;
|
||||
|
@ -7,6 +9,8 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
@ -18,9 +22,25 @@ public class DataIntegrityExceptionTest {
|
|||
@Autowired
|
||||
private IFooService fooService;
|
||||
|
||||
@Autowired
|
||||
private DataSource restDataSource;
|
||||
|
||||
@Test(expected = DataIntegrityViolationException.class)
|
||||
public void whenSavingNullValue_thenDataIntegrityException() {
|
||||
final Foo fooEntity = new Foo();
|
||||
fooService.create(fooEntity);
|
||||
}
|
||||
|
||||
@Test(expected = DuplicateKeyException.class)
|
||||
public void whenSavingDuplicateKeyValues_thenDuplicateKeyException() {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||
|
||||
try {
|
||||
jdbcTemplate.execute("insert into foo(id,name) values (1,'a')");
|
||||
jdbcTemplate.execute("insert into foo(id,name) values (1,'b')");
|
||||
} finally {
|
||||
jdbcTemplate.execute("delete from foo where id=1");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,10 +3,13 @@ package org.baeldung.ex.nontransientdataaccessexception;
|
|||
import javax.sql.DataSource;
|
||||
|
||||
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
@ -25,4 +28,26 @@ public class DataRetrievalExceptionTest {
|
|||
|
||||
jdbcTemplate.queryForObject("select * from foo where id=3", Integer.class);
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectResultSetColumnCountException.class)
|
||||
public void whenRetrievingMultipleColumns_thenIncorrectResultSetColumnCountException() {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||
try {
|
||||
jdbcTemplate.execute("insert into foo(id,name) values (1,'a')");
|
||||
jdbcTemplate.queryForList("select id,name from foo where id=1", Foo.class);
|
||||
} finally {
|
||||
jdbcTemplate.execute("delete from foo where id=1");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectResultSizeDataAccessException.class)
|
||||
public void whenRetrievingMultipleValues_thenIncorrectResultSizeException() {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||
|
||||
jdbcTemplate.execute("insert into foo(name) values ('a')");
|
||||
jdbcTemplate.execute("insert into foo(name) values ('a')");
|
||||
|
||||
jdbcTemplate.queryForObject("select id from foo where name='a'", Integer.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue