Skip to content

Commit d94211f

Browse files
committed
added flexy pool, better transaction management - removed unnecessary @transactional for better performance
1 parent bdc874d commit d94211f

File tree

10 files changed

+33
-20
lines changed

10 files changed

+33
-20
lines changed

main-app/main-webapp/pom.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@
267267
<groupId>net.ttddyy.observation</groupId>
268268
<artifactId>datasource-micrometer-spring-boot</artifactId>
269269
</dependency>
270-
270+
<dependency>
271+
<groupId>com.github.gavlyukovskiy</groupId>
272+
<artifactId>flexy-pool-spring-boot-starter</artifactId>
273+
</dependency>
271274
<!-- spock deps -->
272275
<dependency>
273276
<groupId>org.spockframework</groupId>

main-app/main-webapp/src/main/java/gt/app/modules/article/ArticleService.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.jms.core.JmsTemplate;
1717
import org.springframework.stereotype.Service;
1818
import org.springframework.transaction.annotation.Transactional;
19+
import org.springframework.transaction.support.TransactionTemplate;
1920
import org.springframework.web.multipart.MultipartFile;
2021

2122
import java.util.ArrayList;
@@ -26,7 +27,6 @@
2627
@Service
2728
@Slf4j
2829
@RequiredArgsConstructor
29-
@Transactional
3030
public class ArticleService {
3131

3232
private static final ReceivedFile.FileGroup FILE_GROUP = ReceivedFile.FileGroup.NOTE_ATTACHMENT;
@@ -36,6 +36,7 @@ public class ArticleService {
3636
private final CommentRepository commentRepo;
3737
private final ContentCheckService contentCheckService;
3838
private final WebsocketHandler websocketHandler;
39+
private final TransactionTemplate transactionTemplate;
3940

4041
public Article createArticle(ArticleCreateDto dto) {
4142

@@ -54,15 +55,16 @@ public Article createArticle(ArticleCreateDto dto) {
5455
article.getAttachedFiles().addAll(files);
5556
article.setStatus(ArticleStatus.UNDER_AUTO_REVIEW);
5657

57-
articleRepository.save(article);
58-
59-
contentCheckService.sendForAutoContentReview(article);
58+
transactionTemplate.executeWithoutResult(status -> {
59+
articleRepository.save(article);
60+
contentCheckService.sendForAutoContentReview(article);
61+
});
6062

6163
return article;
6264
}
6365

66+
//single update statement - no transaction required
6467
public Article update(ArticleEditDto dto) {
65-
6668
Optional<Article> articleOpt = articleRepository.findWithFilesAndUserById(dto.getId());
6769
return articleOpt.map(article -> {
6870
ArticleMapper.INSTANCE.createToEntity(dto, article);
@@ -142,12 +144,13 @@ public UUID findCreatedByUserIdById(Long articleId) {
142144
return articleRepository.findCreatedByUserIdById(articleId);
143145
}
144146

145-
@Transactional
146147
public Optional<Article> handleReview(ArticleReviewResultDto dto) {
147148
return articleRepository.findWithModifiedUserByIdAndStatus(dto.getId(), ArticleStatus.FLAGGED_FOR_MANUAL_REVIEW)
148149
.map(n -> {
149-
n.setStatus(dto.getVerdict());
150-
articleRepository.save(n);
150+
transactionTemplate.executeWithoutResult(txSt -> {
151+
n.setStatus(dto.getVerdict());
152+
articleRepository.save(n);
153+
});
151154
websocketHandler.sendToUser(n.getLastModifiedByUser().getUsername(), "Your article with title " + n.getTitle() + " has been " + (dto.getVerdict() == ArticleStatus.PUBLISHED ? "approved from manual review." : "rejected from manual review."));
152155
return n;
153156
});

main-app/main-webapp/src/main/java/gt/app/modules/article/CommentService.java

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class CommentService {
1616
final CommentRepository commentRepository;
1717
final ContentCheckService contentCheckService;
1818

19+
//no transaction required - single operation
1920
public void save(NewCommentDto c) {
2021
Comment comment = new Comment(c.content, c.articleId);
2122

main-app/main-webapp/src/main/java/gt/app/modules/review/ArticleReviewResponseService.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ArticleReviewResponseService {
2727
private final AppProperties appProperties;
2828
private final WebsocketHandler websocketHandler;
2929

30+
//no transaction required here - single operation
3031
void handle(Response resp) {
3132
Article a = articleRepository.findOneWithUserById(Long.valueOf(resp.getEntityId())).orElseThrow();
3233
switch (resp.getContentCheckOutcome()) {

main-app/main-webapp/src/main/java/gt/app/modules/user/UserAuthorityService.java

-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
import lombok.RequiredArgsConstructor;
77
import lombok.extern.slf4j.Slf4j;
88
import org.springframework.stereotype.Service;
9-
import org.springframework.transaction.annotation.Transactional;
109

1110
import java.util.UUID;
1211

1312
@Service("appSecurity")
1413
@RequiredArgsConstructor
1514
@Slf4j
16-
@Transactional(readOnly = true)
1715
public class UserAuthorityService {
1816

1917
private final ArticleService articleService;

main-app/main-webapp/src/main/java/gt/app/modules/user/UserService.java

-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import lombok.RequiredArgsConstructor;
55
import lombok.extern.slf4j.Slf4j;
66
import org.springframework.stereotype.Service;
7-
import org.springframework.transaction.annotation.Transactional;
87

98
import java.util.UUID;
109

1110
@Service
12-
@Transactional
1311
@RequiredArgsConstructor
1412
@Slf4j
1513
public class UserService {

main-app/main-webapp/src/main/resources/application.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ logging:
4444
web: INFO
4545
cloud: INFO
4646
sql: INFO
47-
ROOT: WARN
47+
ROOT: INFO
4848
gt: TRACE
4949
# 'org.springframework.web.socket': TRACE
5050

@@ -69,7 +69,12 @@ wro4j:
6969
# note that both `jdbc.datasource-proxy` and `decorator.datasource` cannot be enabled at same time due to bean name conflict
7070
decorator:
7171
datasource:
72-
enabled: false # disabled for now
72+
enabled: true # disabled for now
73+
flexy-pool:
74+
threshold:
75+
connection:
76+
acquire: -1
77+
lease: 0
7378
datasource-proxy:
7479
slow-query:
7580
threshold: 1 # 1 second is slow
@@ -78,7 +83,7 @@ decorator:
7883
# https://github.com/jdbc-observations/datasource-micrometer
7984
jdbc:
8085
datasource-proxy:
81-
enabled: true
86+
enabled: false
8287

8388
management:
8489
tracing:

main-app/main-webapp/src/test/groovy/gt/app/modules/article/ArticleServiceSpec.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ArticleServiceSpec extends Specification {
2323
commentRepo = Mock()
2424
commentRepo = Mock()
2525
contentCheckRequestService = Mock()
26-
articleService = new ArticleService(articleRepository, fileService, jmsTemplate, commentRepo, contentCheckRequestService, null)
26+
articleService = new ArticleService(articleRepository, fileService, jmsTemplate, commentRepo, contentCheckRequestService, null, null)
2727
}
2828

2929
def 'save article'() {

main-app/main-webapp/src/test/java/gt/app/modules/article/ArticleServiceTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void testMapNested() {
3333
*/
3434

3535
ArticleReadDto flat = getTestArticle();
36-
var service = new ArticleService(null, null, null, null, null, null);
36+
var service = new ArticleService(null, null, null, null, null, null,null);
3737

3838
ArticleReadDto nested = service.mapNested(flat);
3939

pom.xml

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<spring-cloud.version>2024.0.0</spring-cloud.version>
3535
<hibernate-types60.version>2.21.1</hibernate-types60.version>
3636
<hibernate-hypersistence-utils.version>3.6.1</hibernate-hypersistence-utils.version>
37-
<datasource-proxy-sb-starter.version>1.9.2</datasource-proxy-sb-starter.version>
37+
<datasource-proxy-sb-starter.version>1.10.0</datasource-proxy-sb-starter.version>
3838
<datasource-micrometer-sb-starter.version>1.0.5</datasource-micrometer-sb-starter.version>
3939
<springdoc-openapi-ui.version>2.8.5</springdoc-openapi-ui.version>
4040
<mapstruct.version>1.6.2</mapstruct.version>
@@ -160,7 +160,11 @@
160160
<artifactId>datasource-micrometer-spring-boot</artifactId>
161161
<version>${datasource-micrometer-sb-starter.version}</version>
162162
</dependency>
163-
163+
<dependency>
164+
<groupId>com.github.gavlyukovskiy</groupId>
165+
<artifactId>flexy-pool-spring-boot-starter</artifactId>
166+
<version>${datasource-proxy-sb-starter.version}</version>
167+
</dependency>
164168
<dependency>
165169
<groupId>com.tngtech.archunit</groupId>
166170
<artifactId>archunit</artifactId>

0 commit comments

Comments
 (0)