Skip to content

Commit 1fe5598

Browse files
committed
Count and assert SQL
1 parent d59c109 commit 1fe5598

File tree

9 files changed

+216
-203
lines changed

9 files changed

+216
-203
lines changed

HibernateSpringBootCountSQLStatements/pom.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.jpa</groupId>
7-
<artifactId>jpa</artifactId>
7+
<artifactId>HibernateSpringBootCountSQLStatements</artifactId>
88
<version>1.0</version>
99
<packaging>jar</packaging>
1010

11-
<name>jpa</name>
11+
<name>HibernateSpringBootCountSQLStatements</name>
1212
<description>JPA project for Spring Boot</description>
1313

1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>2.0.5.RELEASE</version>
17+
<version>2.1.4.RELEASE</version>
1818
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

@@ -70,6 +70,4 @@
7070
</plugin>
7171
</plugins>
7272
</build>
73-
74-
7573
</project>

HibernateSpringBootCountSQLStatements/src/main/java/com/jpa/CountSQLStatementsApplication.java renamed to HibernateSpringBootCountSQLStatements/src/main/java/com/bookstore/MainApplication.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
package com.jpa;
1+
package com.bookstore;
22

3+
import com.bookstore.service.BookstoreService;
34
import com.vladmihalcea.sql.SQLStatementCountValidator;
45
import static com.vladmihalcea.sql.SQLStatementCountValidator.assertDeleteCount;
56
import static com.vladmihalcea.sql.SQLStatementCountValidator.assertInsertCount;
67
import static com.vladmihalcea.sql.SQLStatementCountValidator.assertSelectCount;
78
import static com.vladmihalcea.sql.SQLStatementCountValidator.assertUpdateCount;
8-
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.boot.ApplicationRunner;
1010
import org.springframework.boot.SpringApplication;
1111
import org.springframework.boot.autoconfigure.SpringBootApplication;
1212
import org.springframework.context.annotation.Bean;
1313

1414
@SpringBootApplication
15-
public class CountSQLStatementsApplication {
15+
public class MainApplication {
1616

17-
@Autowired
18-
private UserService userService;
17+
private final BookstoreService bookstoreService;
18+
19+
public MainApplication(BookstoreService bookstoreService) {
20+
this.bookstoreService = bookstoreService;
21+
}
1922

2023
public static void main(String[] args) {
21-
SpringApplication.run(CountSQLStatementsApplication.class, args);
24+
SpringApplication.run(MainApplication.class, args);
2225
}
2326

2427
@Bean
2528
public ApplicationRunner init() {
2629
return args -> {
27-
28-
userService.userOperationsWithoutTransactional();
30+
31+
bookstoreService.authorOperationsWithoutTransactional();
2932

3033
SQLStatementCountValidator.reset();
31-
userService.userOperationsWithTransactional();
34+
bookstoreService.authorOperationsWithTransactional();
3235

3336
// allow the transaction to commit
3437
// a total of 2 statements instead of 5 as in the case of no explicit transaction

HibernateSpringBootCountSQLStatements/src/main/java/com/jpa/DatasourceProxyBeanPostProcessor.java renamed to HibernateSpringBootCountSQLStatements/src/main/java/com/bookstore/config/DatasourceProxyBeanPostProcessor.java

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
package com.jpa;
2-
3-
import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;
4-
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
5-
import org.aopalliance.intercept.MethodInterceptor;
6-
import org.aopalliance.intercept.MethodInvocation;
7-
import org.springframework.aop.framework.ProxyFactory;
8-
import org.springframework.beans.factory.config.BeanPostProcessor;
9-
import org.springframework.stereotype.Component;
10-
import org.springframework.util.ReflectionUtils;
11-
12-
import javax.sql.DataSource;
13-
import java.lang.reflect.Method;
14-
import java.util.logging.Logger;
15-
16-
@Component
17-
public class DatasourceProxyBeanPostProcessor implements BeanPostProcessor {
18-
19-
private static final Logger logger
20-
= Logger.getLogger(DatasourceProxyBeanPostProcessor.class.getName());
21-
22-
@Override
23-
public Object postProcessAfterInitialization(Object bean, String beanName) {
24-
25-
if (bean instanceof DataSource) {
26-
27-
logger.info(() -> "DataSource bean has been found: " + bean);
28-
29-
final ProxyFactory proxyFactory = new ProxyFactory(bean);
30-
31-
proxyFactory.setProxyTargetClass(true);
32-
proxyFactory.addAdvice(new ProxyDataSourceInterceptor((DataSource) bean));
33-
34-
return proxyFactory.getProxy();
35-
}
36-
return bean;
37-
}
38-
39-
@Override
40-
public Object postProcessBeforeInitialization(Object bean, String beanName) {
41-
return bean;
42-
}
43-
44-
private static class ProxyDataSourceInterceptor implements MethodInterceptor {
45-
46-
private final DataSource dataSource;
47-
48-
public ProxyDataSourceInterceptor(final DataSource dataSource) {
49-
super();
50-
this.dataSource = ProxyDataSourceBuilder.create(dataSource)
51-
.name("DATA_SOURCE_PROXY")
52-
.logQueryBySlf4j(SLF4JLogLevel.INFO)
53-
.multiline()
54-
.countQuery()
55-
.build();
56-
}
57-
58-
@Override
59-
public Object invoke(final MethodInvocation invocation) throws Throwable {
60-
61-
final Method proxyMethod = ReflectionUtils.
62-
findMethod(this.dataSource.getClass(),
63-
invocation.getMethod().getName());
64-
65-
if (proxyMethod != null) {
66-
return proxyMethod.invoke(this.dataSource, invocation.getArguments());
67-
}
68-
69-
return invocation.proceed();
70-
}
71-
}
72-
}
1+
package com.bookstore.config;
2+
3+
import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;
4+
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
5+
import org.aopalliance.intercept.MethodInterceptor;
6+
import org.aopalliance.intercept.MethodInvocation;
7+
import org.springframework.aop.framework.ProxyFactory;
8+
import org.springframework.beans.factory.config.BeanPostProcessor;
9+
import org.springframework.util.ReflectionUtils;
10+
11+
import javax.sql.DataSource;
12+
import java.lang.reflect.Method;
13+
import java.util.logging.Logger;
14+
import org.springframework.context.annotation.Configuration;
15+
16+
@Configuration
17+
public class DatasourceProxyBeanPostProcessor implements BeanPostProcessor {
18+
19+
private static final Logger logger
20+
= Logger.getLogger(DatasourceProxyBeanPostProcessor.class.getName());
21+
22+
@Override
23+
public Object postProcessAfterInitialization(Object bean, String beanName) {
24+
25+
if (bean instanceof DataSource) {
26+
27+
logger.info(() -> "DataSource bean has been found: " + bean);
28+
29+
final ProxyFactory proxyFactory = new ProxyFactory(bean);
30+
31+
proxyFactory.setProxyTargetClass(true);
32+
proxyFactory.addAdvice(new ProxyDataSourceInterceptor((DataSource) bean));
33+
34+
return proxyFactory.getProxy();
35+
}
36+
return bean;
37+
}
38+
39+
@Override
40+
public Object postProcessBeforeInitialization(Object bean, String beanName) {
41+
return bean;
42+
}
43+
44+
private static class ProxyDataSourceInterceptor implements MethodInterceptor {
45+
46+
private final DataSource dataSource;
47+
48+
public ProxyDataSourceInterceptor(final DataSource dataSource) {
49+
super();
50+
this.dataSource = ProxyDataSourceBuilder.create(dataSource)
51+
.name("DATA_SOURCE_PROXY")
52+
.logQueryBySlf4j(SLF4JLogLevel.INFO)
53+
.multiline()
54+
.countQuery()
55+
.build();
56+
}
57+
58+
@Override
59+
public Object invoke(final MethodInvocation invocation) throws Throwable {
60+
61+
final Method proxyMethod = ReflectionUtils.
62+
findMethod(this.dataSource.getClass(),
63+
invocation.getMethod().getName());
64+
65+
if (proxyMethod != null) {
66+
return proxyMethod.invoke(this.dataSource, invocation.getArguments());
67+
}
68+
69+
return invocation.proceed();
70+
}
71+
}
72+
}

HibernateSpringBootCountSQLStatements/src/main/java/com/jpa/User.java renamed to HibernateSpringBootCountSQLStatements/src/main/java/com/bookstore/entity/Author.java

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,59 @@
1-
package com.jpa;
2-
3-
import java.io.Serializable;
4-
import javax.persistence.Entity;
5-
import javax.persistence.GeneratedValue;
6-
import javax.persistence.GenerationType;
7-
import javax.persistence.Id;
8-
9-
@Entity
10-
public class User implements Serializable {
11-
12-
private static final long serialVersionUID = 1L;
13-
14-
@Id
15-
@GeneratedValue(strategy = GenerationType.IDENTITY)
16-
private Long id;
17-
18-
private String name;
19-
private String city;
20-
private int age;
21-
22-
public Long getId() {
23-
return id;
24-
}
25-
26-
public void setId(Long id) {
27-
this.id = id;
28-
}
29-
30-
public String getName() {
31-
return name;
32-
}
33-
34-
public void setName(String name) {
35-
this.name = name;
36-
}
37-
38-
public String getCity() {
39-
return city;
40-
}
41-
42-
public void setCity(String city) {
43-
this.city = city;
44-
}
45-
46-
public int getAge() {
47-
return age;
48-
}
49-
50-
public void setAge(int age) {
51-
this.age = age;
52-
}
53-
54-
}
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Author implements Serializable {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
18+
private int age;
19+
private String name;
20+
private String genre;
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getGenre() {
39+
return genre;
40+
}
41+
42+
public void setGenre(String genre) {
43+
this.genre = genre;
44+
}
45+
46+
public int getAge() {
47+
return age;
48+
}
49+
50+
public void setAge(int age) {
51+
this.age = age;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Author{" + "id=" + id + ", age=" + age
57+
+ ", name=" + name + ", genre=" + genre + '}';
58+
}
59+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Author;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface AuthorRepository extends JpaRepository<Author, Long> {
9+
}

0 commit comments

Comments
 (0)