Skip to content

Commit e85dc04

Browse files
committed
added samples
1 parent 82f0782 commit e85dc04

File tree

92 files changed

+3972
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+3972
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
*.class
22

3+
.idea
4+
*.iml
5+
36
# Mobile Tools for Java (J2ME)
47
.mtj.tmp/
58

database-orm/common/pom.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<parent>
4+
<artifactId>database-orm</artifactId>
5+
<groupId>dev.gsitgithub</groupId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<artifactId>common</artifactId>
11+
<packaging>jar</packaging>
12+
13+
<name>common</name>
14+
<url>http://maven.apache.org</url>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- HikariCP -->
22+
<dependency>
23+
<groupId>com.zaxxer</groupId>
24+
<artifactId>HikariCP</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>3.8.1</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.eclipse.persistence</groupId>
35+
<artifactId>javax.persistence</artifactId>
36+
</dependency>
37+
38+
</dependencies>
39+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dev.gsitgithub.db;
2+
3+
import com.zaxxer.hikari.HikariConfig;
4+
import com.zaxxer.hikari.HikariDataSource;
5+
6+
// {{start:poolFactory}}
7+
class ConnectionPool {
8+
private ConnectionPool() {
9+
10+
}
11+
/*
12+
* Expects a config in the following format
13+
*
14+
* poolName = "test pool"
15+
* jdbcUrl = ""
16+
* maximumPoolSize = 10
17+
* minimumIdle = 2
18+
* username = ""
19+
* password = ""
20+
* cachePrepStmts = true
21+
* prepStmtCacheSize = 256
22+
* prepStmtCacheSqlLimit = 2048
23+
* useServerPrepStmts = true
24+
*
25+
* Let HikariCP bleed out here on purpose
26+
*/
27+
public static HikariDataSource getDataSourceFromConfig( String poolName, int maximumPoolSize,
28+
int minimumIdle) {
29+
30+
HikariConfig jdbcConfig = new HikariConfig();
31+
jdbcConfig.setPoolName(poolName);
32+
jdbcConfig.setMaximumPoolSize(maximumPoolSize);
33+
jdbcConfig.setMinimumIdle( minimumIdle );
34+
jdbcConfig.setJdbcUrl("jdbcurl");
35+
jdbcConfig.setUsername("username");
36+
jdbcConfig.setPassword("password");
37+
38+
jdbcConfig.addDataSourceProperty("cachePrepStmts", true);
39+
jdbcConfig.addDataSourceProperty("prepStmtCacheSize", 250);
40+
jdbcConfig.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
41+
jdbcConfig.addDataSourceProperty("useServerPrepStmts", true);
42+
43+
// Add HealthCheck
44+
//jdbcConfig.setHealthCheckRegistry(healthCheckRegistry);
45+
46+
// Add Metrics
47+
//jdbcConfig.setMetricRegistry(metricRegistry);
48+
return new HikariDataSource(jdbcConfig);
49+
}
50+
}
51+
// {{end:poolFactory}}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package dev.gsitgithub.db;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
// {{start:pools}}
9+
public class ConnectionPools {
10+
private static final Logger logger = LoggerFactory.getLogger(ConnectionPools.class);
11+
/*
12+
* Normally we would be using the app config but since this is an example
13+
* we will be using a localized example config.
14+
*/
15+
/*
16+
* This pool is made for short quick transactions that the web application uses.
17+
* Using enum singleton pattern for lazy singletons
18+
*/
19+
private enum Transactional {
20+
INSTANCE(ConnectionPool.getDataSourceFromConfig("transactional", 10,2));
21+
private final DataSource dataSource;
22+
private Transactional(DataSource dataSource) {
23+
this.dataSource = dataSource;
24+
}
25+
public DataSource getDataSource() {
26+
return dataSource;
27+
}
28+
}
29+
public static DataSource getTransactional() {
30+
return Transactional.INSTANCE.getDataSource();
31+
}
32+
33+
/*
34+
* This pool is designed for longer running transactions / bulk inserts / background jobs
35+
* Basically if you have any multithreading or long running background jobs
36+
* you do not want to starve the main applications connection pool.
37+
*
38+
* EX.
39+
* You have an endpoint that needs to insert 1000 db records
40+
* This will queue up all the connections in the pool
41+
*
42+
* While this is happening a user tries to log into the site.
43+
* If you use the same pool they may be blocked until the bulk insert is done
44+
* By splitting pools you can give transactional queries a much higher chance to
45+
* run while the other pool is backed up.
46+
*/
47+
private enum Processing {
48+
INSTANCE(ConnectionPool.getDataSourceFromConfig("processing", 30, 2));
49+
private final DataSource dataSource;
50+
private Processing(DataSource dataSource) {
51+
this.dataSource = dataSource;
52+
}
53+
public DataSource getDataSource() {
54+
return dataSource;
55+
}
56+
}
57+
58+
public static DataSource getProcessing() {
59+
return Processing.INSTANCE.getDataSource();
60+
}
61+
62+
public static void main(String[] args) {
63+
logger.debug("starting");
64+
DataSource processing = ConnectionPools.getProcessing();
65+
logger.debug("processing started");
66+
DataSource transactional = ConnectionPools.getTransactional();
67+
logger.debug("transactional started");
68+
logger.debug("done");
69+
}
70+
}
71+
// {{end:pools}}
72+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dev.gsitgithub.entity;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
import javax.persistence.Table;
8+
9+
//import org.hibernate.annotations.GenericGenerator;
10+
11+
12+
@Entity
13+
@Table(name = "authority")
14+
public class Authority {
15+
16+
@Id
17+
//@GenericGenerator(name = "generator", strategy = "increment")
18+
@GeneratedValue(generator = "generator")
19+
private Long id;
20+
21+
@Column(name = "name", nullable = false)
22+
private String name;
23+
24+
public Authority(long a_id, String a_name) {
25+
this.id = a_id;
26+
this.name = a_name;
27+
}
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
45+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package dev.gsitgithub.entity;
2+
3+
4+
import javax.persistence.Column;
5+
import javax.persistence.Entity;
6+
import javax.persistence.Id;
7+
import javax.persistence.Table;
8+
import java.io.Serializable;
9+
import java.util.Date;
10+
11+
@Entity
12+
@Table(name = "token")
13+
public class Token implements Serializable {
14+
15+
16+
@Id
17+
private String series;
18+
19+
private String value;
20+
21+
private Date date;
22+
23+
@Column(name = "ip_address")
24+
private String ipAddress;
25+
26+
@Column(name = "user_agent")
27+
private String userAgent;
28+
29+
@Column(name = "user_login")
30+
private String userLogin;
31+
32+
public String getSeries() {
33+
return series;
34+
}
35+
36+
public void setSeries(String series) {
37+
this.series = series;
38+
}
39+
40+
public String getValue() {
41+
return value;
42+
}
43+
44+
public void setValue(String value) {
45+
this.value = value;
46+
}
47+
48+
public Date getDate() {
49+
return date;
50+
}
51+
52+
public void setDate(Date date) {
53+
this.date = date;
54+
}
55+
56+
public String getIpAddress() {
57+
return ipAddress;
58+
}
59+
60+
public void setIpAddress(String ipAddress) {
61+
this.ipAddress = ipAddress;
62+
}
63+
64+
public String getUserAgent() {
65+
return userAgent;
66+
}
67+
68+
public String getUserLogin() {
69+
return userLogin;
70+
}
71+
72+
public void setUserLogin(String userLogin) {
73+
this.userLogin = userLogin;
74+
}
75+
76+
public void setUserAgent(String userAgent) {
77+
this.userAgent = userAgent;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)