Skip to content

Commit 2817953

Browse files
lor6pivovarit
authored andcommitted
Non transient exception (eugenp#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
1 parent 8a10405 commit 2817953

6 files changed

Lines changed: 161 additions & 43 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.baeldung.ex.nontransientexception.cause;
2+
3+
import java.util.Properties;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.ComponentScan;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.context.annotation.PropertySource;
13+
import org.springframework.core.env.Environment;
14+
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
15+
import org.springframework.orm.hibernate4.HibernateTransactionManager;
16+
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
17+
import org.springframework.transaction.annotation.EnableTransactionManagement;
18+
19+
import com.google.common.base.Preconditions;
20+
21+
@Configuration
22+
@EnableTransactionManagement
23+
@PropertySource({ "classpath:persistence-mysql-incorrect.properties" })
24+
@ComponentScan({ "org.baeldung.persistence" })
25+
public class Cause5NonTransientConfig {
26+
27+
@Autowired
28+
private Environment env;
29+
30+
public Cause5NonTransientConfig() {
31+
super();
32+
}
33+
34+
@Bean
35+
public LocalSessionFactoryBean sessionFactory() {
36+
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
37+
sessionFactory.setDataSource(restDataSource());
38+
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
39+
sessionFactory.setHibernateProperties(hibernateProperties());
40+
41+
return sessionFactory;
42+
}
43+
44+
@Bean
45+
public DataSource restDataSource() {
46+
final BasicDataSource dataSource = new BasicDataSource();
47+
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
48+
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
49+
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
50+
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
51+
52+
return dataSource;
53+
}
54+
55+
@Bean
56+
public HibernateTransactionManager transactionManager() {
57+
final HibernateTransactionManager txManager = new HibernateTransactionManager();
58+
txManager.setSessionFactory(sessionFactory().getObject());
59+
60+
return txManager;
61+
}
62+
63+
@Bean
64+
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
65+
return new PersistenceExceptionTranslationPostProcessor();
66+
}
67+
68+
final Properties hibernateProperties() {
69+
final Properties hibernateProperties = new Properties();
70+
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
71+
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
72+
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
73+
return hibernateProperties;
74+
}
75+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# jdbc.X
2+
jdbc.driverClassName=com.mysql.jdbc.Driver
3+
jdbc.url=jdbc:mysql:3306://localhost/spring_hibernate4_exceptions?createDatabaseIfNotExist=true
4+
jdbc.user=tutorialuser
5+
jdbc.pass=tutorialmy5ql
6+
7+
# hibernate.X
8+
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
9+
hibernate.show_sql=false
10+
hibernate.hbm2ddl.auto=create
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.ex.nontransientdataaccessexception;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
6+
import org.baeldung.ex.nontransientexception.cause.Cause5NonTransientConfig;
7+
import org.baeldung.persistence.model.Foo;
8+
import org.baeldung.persistence.service.IFooService;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.dao.DataIntegrityViolationException;
13+
import org.springframework.jdbc.CannotGetJdbcConnectionException;
14+
import org.springframework.jdbc.core.JdbcTemplate;
15+
import org.springframework.test.context.ContextConfiguration;
16+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
18+
19+
@RunWith(SpringJUnit4ClassRunner.class)
20+
@ContextConfiguration(classes = { Cause5NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
21+
public class CannotGetJdbcConnectionExceptionTest {
22+
23+
@Autowired
24+
private DataSource restDataSource;
25+
26+
@Test(expected = CannotGetJdbcConnectionException.class)
27+
public void whenJdbcUrlIncorrect_thenCannotGetJdbcConnectionException() {
28+
JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
29+
jdbcTemplate.execute("select * from foo");
30+
}
31+
}

‎spring-exceptions/src/test/java/org/baeldung/ex/nontransientdataaccessexception/CleanupFailureExceptionTest.java‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎spring-exceptions/src/test/java/org/baeldung/ex/nontransientdataaccessexception/DataIntegrityExceptionTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.baeldung.ex.nontransientdataaccessexception;
22

3+
import javax.sql.DataSource;
4+
35
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
46
import org.baeldung.persistence.model.Foo;
57
import org.baeldung.persistence.service.IFooService;
68
import org.junit.Test;
79
import org.junit.runner.RunWith;
810
import org.springframework.beans.factory.annotation.Autowired;
911
import org.springframework.dao.DataIntegrityViolationException;
12+
import org.springframework.dao.DuplicateKeyException;
13+
import org.springframework.jdbc.core.JdbcTemplate;
1014
import org.springframework.test.context.ContextConfiguration;
1115
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1216
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@@ -18,9 +22,25 @@ public class DataIntegrityExceptionTest {
1822
@Autowired
1923
private IFooService fooService;
2024

25+
@Autowired
26+
private DataSource restDataSource;
27+
2128
@Test(expected = DataIntegrityViolationException.class)
2229
public void whenSavingNullValue_thenDataIntegrityException() {
2330
final Foo fooEntity = new Foo();
2431
fooService.create(fooEntity);
2532
}
33+
34+
@Test(expected = DuplicateKeyException.class)
35+
public void whenSavingDuplicateKeyValues_thenDuplicateKeyException() {
36+
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
37+
38+
try {
39+
jdbcTemplate.execute("insert into foo(id,name) values (1,'a')");
40+
jdbcTemplate.execute("insert into foo(id,name) values (1,'b')");
41+
} finally {
42+
jdbcTemplate.execute("delete from foo where id=1");
43+
}
44+
}
45+
2646
}

‎spring-exceptions/src/test/java/org/baeldung/ex/nontransientdataaccessexception/DataRetrievalExceptionTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import javax.sql.DataSource;
44

55
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
6+
import org.baeldung.persistence.model.Foo;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.dao.DataRetrievalFailureException;
11+
import org.springframework.dao.IncorrectResultSizeDataAccessException;
12+
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
1013
import org.springframework.jdbc.core.JdbcTemplate;
1114
import org.springframework.test.context.ContextConfiguration;
1215
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -25,4 +28,26 @@ public void whenRetrievingNonExistentValue_thenDataRetrievalException() {
2528

2629
jdbcTemplate.queryForObject("select * from foo where id=3", Integer.class);
2730
}
31+
32+
@Test(expected = IncorrectResultSetColumnCountException.class)
33+
public void whenRetrievingMultipleColumns_thenIncorrectResultSetColumnCountException() {
34+
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
35+
try {
36+
jdbcTemplate.execute("insert into foo(id,name) values (1,'a')");
37+
jdbcTemplate.queryForList("select id,name from foo where id=1", Foo.class);
38+
} finally {
39+
jdbcTemplate.execute("delete from foo where id=1");
40+
}
41+
}
42+
43+
@Test(expected = IncorrectResultSizeDataAccessException.class)
44+
public void whenRetrievingMultipleValues_thenIncorrectResultSizeException() {
45+
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
46+
47+
jdbcTemplate.execute("insert into foo(name) values ('a')");
48+
jdbcTemplate.execute("insert into foo(name) values ('a')");
49+
50+
jdbcTemplate.queryForObject("select id from foo where name='a'", Integer.class);
51+
}
52+
2853
}

0 commit comments

Comments
 (0)