Skip to content

Commit 9c99f13

Browse files
Add files via upload
1 parent 489ebd7 commit 9c99f13

File tree

8 files changed

+251
-0
lines changed

8 files changed

+251
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>jpa</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>jpa</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.1.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
26+
<datasource-proxy.version>1.4.9</datasource-proxy.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-jdbc</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter</artifactId>
45+
<exclusions>
46+
<exclusion>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-logging</artifactId>
49+
</exclusion>
50+
</exclusions>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-log4j2</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>mysql</groupId>
58+
<artifactId>mysql-connector-java</artifactId>
59+
<scope>runtime</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-test</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
67+
<dependency>
68+
<groupId>c3p0</groupId>
69+
<artifactId>c3p0</artifactId>
70+
<version>0.9.1.2</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-configuration-processor</artifactId>
75+
<optional>true</optional>
76+
</dependency>
77+
</dependencies>
78+
79+
<build>
80+
<plugins>
81+
<plugin>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-maven-plugin</artifactId>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
88+
89+
</project>
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.jpa;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.ApplicationRunner;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.context.ApplicationContext;
11+
import org.springframework.context.annotation.Bean;
12+
13+
@SpringBootApplication
14+
public class C3P0Application {
15+
16+
private static final ExecutorService executor = Executors.newFixedThreadPool(25);
17+
18+
@Autowired
19+
private ApplicationContext applicationContext;
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(C3P0Application.class, args);
23+
}
24+
25+
@Bean
26+
public ApplicationRunner init() {
27+
return args -> {
28+
29+
while(true) {
30+
SampleRepository sampleThread
31+
= applicationContext.getBean(SampleRepository.class);
32+
executor.execute(sampleThread);
33+
34+
Thread.sleep(new Random().nextInt(125));
35+
}
36+
37+
};
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.jpa;
2+
3+
import com.mchange.v2.c3p0.ComboPooledDataSource;
4+
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.Primary;
9+
10+
@Configuration
11+
public class ConfigureDataSource {
12+
13+
@Bean
14+
@Primary
15+
@ConfigurationProperties("app.datasource")
16+
public DataSourceProperties dataSourceProperties() {
17+
return new DataSourceProperties();
18+
}
19+
20+
@Bean
21+
@ConfigurationProperties("app.datasource")
22+
public ComboPooledDataSource dataSource(DataSourceProperties properties) {
23+
return properties.initializeDataSourceBuilder().type(ComboPooledDataSource.class)
24+
.build();
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
import java.sql.Connection;
5+
import java.sql.PreparedStatement;
6+
import java.sql.SQLException;
7+
import java.util.Random;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
10+
import javax.persistence.EntityManager;
11+
import javax.persistence.PersistenceContext;
12+
import org.hibernate.Session;
13+
import org.springframework.context.annotation.Scope;
14+
import org.springframework.stereotype.Repository;
15+
import org.springframework.transaction.annotation.Transactional;
16+
17+
@Repository
18+
@Transactional
19+
@Scope("prototype")
20+
public class SampleRepository implements Serializable, Runnable {
21+
22+
private static final Logger logger = Logger.getLogger(SampleRepository.class.getName());
23+
private static final String SQL_INSERT = "INSERT INTO samples (sample) VALUES (?)";
24+
25+
@PersistenceContext
26+
private EntityManager entityManager;
27+
28+
@Override
29+
public void run() {
30+
Session hibernateSession = entityManager.unwrap(Session.class);
31+
hibernateSession.doWork(this::insertSample);
32+
}
33+
34+
public void insertSample(Connection connection) {
35+
try (PreparedStatement preparedStatement = connection.prepareStatement(SQL_INSERT)) {
36+
37+
preparedStatement.setInt(1, new Random().nextInt());
38+
preparedStatement.execute();
39+
40+
Thread.sleep(new Random().nextInt(100));
41+
42+
logger.log(Level.INFO, "Processed by {0}", Thread.currentThread().getName());
43+
44+
} catch (SQLException e) {
45+
logger.log(Level.SEVERE, "SQL exception", e);
46+
} catch (InterruptedException ex) {
47+
Thread.currentThread().interrupt();
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
app.datasource.url=jdbc:mysql://localhost:3306/samples_db?createDatabaseIfNotExist=true
2+
app.datasource.user=root
3+
app.datasource.password=root
4+
app.datasource.initial-pool-size=10
5+
app.datasource.min-pool-size=10
6+
app.datasource.max-pool-size=20
7+
app.datasource.max-idle-time=0
8+
app.datasource.acquire-increment=1
9+
# enable auto-commit (disabled by default)
10+
#app.datasource.auto-commit-on-close=false
11+
# more settings can be added as app.datasource.*
12+
13+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
14+
15+
spring.datasource.initialization-mode=always
16+
spring.datasource.platform=mysql
17+
18+
spring.jpa.hibernate.ddl-auto=create
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Drop
2+
DROP TABLE IF EXISTS `samples_db`.`samples`;
3+
4+
-- Create the table
5+
CREATE TABLE `samples_db`.`samples` (
6+
`sample` INT DEFAULT NULL
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout
6+
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
7+
</Console>
8+
</Appenders>
9+
10+
<Loggers>
11+
<!-- LOG everything at INFO level -->
12+
<Root level="info">
13+
<AppenderRef ref="Console" />
14+
</Root>
15+
16+
<logger name="com.mchange.v2.c3p0.*" additivity="false">
17+
<level value="trace"/>
18+
<appender-ref ref="Console"/>
19+
</logger>
20+
</Loggers>
21+
22+
</Configuration>

0 commit comments

Comments
 (0)