Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -9,9 +9,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-1</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -63,7 +63,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<flyway-core.version>5.0.2</flyway-core.version>
|
||||
<flyway-core.version>5.2.3</flyway-core.version>
|
||||
<flyway-maven-plugin.version>5.0.2</flyway-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
#flyway.enabled=false
|
||||
#spring.flyway.enabled=false
|
||||
@@ -13,3 +13,4 @@ This module contains articles specific to use of Hibernate as a JPA implementati
|
||||
- [Enabling Transaction Locks in Spring Data JPA](https://www.baeldung.com/java-jpa-transaction-locks)
|
||||
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
|
||||
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
|
||||
- [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference)
|
||||
|
||||
@@ -18,6 +18,12 @@
|
||||
<artifactId>cassandra-driver-core</artifactId>
|
||||
<version>${cassandra-driver-core.version}</version>
|
||||
<optional>true</optional>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.cassandraunit/cassandra-unit -->
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
cluster_name: 'Test Cluster'
|
||||
hinted_handoff_enabled: true
|
||||
max_hint_window_in_ms: 10800000
|
||||
hinted_handoff_throttle_in_kb: 1024
|
||||
max_hints_delivery_threads: 2
|
||||
hints_directory: target/embeddedCassandra/hints
|
||||
authenticator: AllowAllAuthenticator
|
||||
authorizer: AllowAllAuthorizer
|
||||
permissions_validity_in_ms: 2000
|
||||
partitioner: RandomPartitioner
|
||||
data_file_directories:
|
||||
- target/embeddedCassandra/data
|
||||
commitlog_directory: target/embeddedCassandra/commitlog
|
||||
cdc_raw_directory: target/embeddedCassandra/cdc
|
||||
disk_failure_policy: stop
|
||||
key_cache_size_in_mb:
|
||||
key_cache_save_period: 14400
|
||||
row_cache_size_in_mb: 0
|
||||
row_cache_save_period: 0
|
||||
saved_caches_directory: target/embeddedCassandra/saved_caches
|
||||
commitlog_sync: periodic
|
||||
commitlog_sync_period_in_ms: 10000
|
||||
commitlog_segment_size_in_mb: 32
|
||||
concurrent_reads: 32
|
||||
concurrent_writes: 32
|
||||
trickle_fsync: false
|
||||
trickle_fsync_interval_in_kb: 10240
|
||||
thrift_framed_transport_size_in_mb: 15
|
||||
thrift_max_message_length_in_mb: 16
|
||||
incremental_backups: false
|
||||
snapshot_before_compaction: false
|
||||
auto_snapshot: false
|
||||
column_index_size_in_kb: 64
|
||||
compaction_throughput_mb_per_sec: 16
|
||||
read_request_timeout_in_ms: 5000
|
||||
range_request_timeout_in_ms: 10000
|
||||
write_request_timeout_in_ms: 2000
|
||||
cas_contention_timeout_in_ms: 1000
|
||||
truncate_request_timeout_in_ms: 60000
|
||||
request_timeout_in_ms: 10000
|
||||
cross_node_timeout: false
|
||||
endpoint_snitch: SimpleSnitch
|
||||
dynamic_snitch_update_interval_in_ms: 100
|
||||
dynamic_snitch_reset_interval_in_ms: 600000
|
||||
dynamic_snitch_badness_threshold: 0.1
|
||||
request_scheduler: org.apache.cassandra.scheduler.NoScheduler
|
||||
index_interval: 128
|
||||
seed_provider:
|
||||
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
|
||||
parameters:
|
||||
- seeds: "127.0.0.1"
|
||||
@@ -10,3 +10,4 @@ This module contains articles about MongoDB in Java.
|
||||
- [Geospatial Support in MongoDB](https://www.baeldung.com/mongodb-geospatial-support)
|
||||
- [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
|
||||
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
|
||||
- [MongoDB BSON to JSON](https://www.baeldung.com/bson-to-json)
|
||||
|
||||
+70
-18
@@ -1,5 +1,7 @@
|
||||
package com.baeldung.morphia.domain;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -29,28 +31,13 @@ public class Book {
|
||||
private double cost;
|
||||
@Reference
|
||||
private Set<Book> companionBooks;
|
||||
@Property
|
||||
private LocalDateTime publishDate;
|
||||
|
||||
public Book() {
|
||||
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public double getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public void addCompanionBooks(Book book) {
|
||||
if (companionBooks != null)
|
||||
this.companionBooks.add(book);
|
||||
}
|
||||
|
||||
public Book(String isbn, String title, String author, double cost, Publisher publisher) {
|
||||
this.isbn = isbn;
|
||||
this.title = title;
|
||||
@@ -60,6 +47,71 @@ public class Book {
|
||||
this.companionBooks = new HashSet<>();
|
||||
}
|
||||
|
||||
// Getters and setters ...
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public Book setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public Book setTitle(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public Book setAuthor(String author) {
|
||||
this.author = author;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Publisher getPublisher() {
|
||||
return publisher;
|
||||
}
|
||||
|
||||
public Book setPublisher(Publisher publisher) {
|
||||
this.publisher = publisher;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public Book setCost(double cost) {
|
||||
this.cost = cost;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LocalDateTime getPublishDate() {
|
||||
return publishDate;
|
||||
}
|
||||
|
||||
public Book setPublishDate(LocalDateTime publishDate) {
|
||||
this.publishDate = publishDate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Set<Book> getCompanionBooks() {
|
||||
return companionBooks;
|
||||
}
|
||||
|
||||
public Book addCompanionBooks(Book book) {
|
||||
if (companionBooks != null)
|
||||
this.companionBooks.add(book);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [isbn=" + isbn + ", title=" + title + ", author=" + author + ", publisher=" + publisher + ", cost=" + cost + "]";
|
||||
@@ -113,4 +165,4 @@ public class Book {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package com.baeldung.bsontojson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.json.Converter;
|
||||
import org.bson.json.JsonMode;
|
||||
import org.bson.json.JsonWriterSettings;
|
||||
import org.bson.json.StrictJsonWriter;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.baeldung.morphia.domain.Book;
|
||||
import com.baeldung.morphia.domain.Publisher;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
|
||||
import dev.morphia.Datastore;
|
||||
import dev.morphia.Morphia;
|
||||
|
||||
public class BsonToJsonIntegrationTest {
|
||||
|
||||
private static final String DB_NAME = "library";
|
||||
private static Datastore datastore;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
Morphia morphia = new Morphia();
|
||||
morphia.mapPackage("com.baeldung.morphia");
|
||||
datastore = morphia.createDatastore(new MongoClient(), DB_NAME);
|
||||
datastore.ensureIndexes();
|
||||
|
||||
datastore.save(new Book()
|
||||
.setIsbn("isbn")
|
||||
.setTitle("title")
|
||||
.setAuthor("author")
|
||||
.setCost(3.95)
|
||||
.setPublisher(new Publisher(new ObjectId("fffffffffffffffffffffffa"),"publisher"))
|
||||
.setPublishDate(LocalDateTime.parse("2020-01-01T18:13:32Z", DateTimeFormatter.ISO_DATE_TIME))
|
||||
.addCompanionBooks(new Book().setIsbn("isbn2")));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
datastore.delete(datastore.createQuery(Book.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBsonDocument_whenUsingStandardJsonTransformation_thenJsonDateIsObjectEpochTime() {
|
||||
|
||||
String json = null;
|
||||
try (MongoClient mongoClient = new MongoClient()) {
|
||||
MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
|
||||
Document bson = mongoDatabase.getCollection("Books").find().first();
|
||||
json = bson.toJson();
|
||||
}
|
||||
|
||||
String expectedJson = "{\"_id\": \"isbn\", " +
|
||||
"\"className\": \"com.baeldung.morphia.domain.Book\", " +
|
||||
"\"title\": \"title\", " +
|
||||
"\"author\": \"author\", " +
|
||||
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
|
||||
"\"name\": \"publisher\"}, " +
|
||||
"\"price\": 3.95, " +
|
||||
"\"publishDate\": {\"$date\": 1577898812000}}";
|
||||
|
||||
assertNotNull(json);
|
||||
|
||||
assertEquals(expectedJson, json);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenBsonDocument_whenUsingRelaxedJsonTransformation_thenJsonDateIsObjectIsoDate() {
|
||||
|
||||
String json = null;
|
||||
try (MongoClient mongoClient = new MongoClient()) {
|
||||
MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
|
||||
Document bson = mongoDatabase.getCollection("Books").find().first();
|
||||
json = bson.toJson(JsonWriterSettings
|
||||
.builder()
|
||||
.outputMode(JsonMode.RELAXED)
|
||||
.build());
|
||||
}
|
||||
|
||||
String expectedJson = "{\"_id\": \"isbn\", " +
|
||||
"\"className\": \"com.baeldung.morphia.domain.Book\", " +
|
||||
"\"title\": \"title\", " +
|
||||
"\"author\": \"author\", " +
|
||||
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
|
||||
"\"name\": \"publisher\"}, " +
|
||||
"\"price\": 3.95, " +
|
||||
"\"publishDate\": {\"$date\": \"2020-01-01T17:13:32Z\"}}";
|
||||
|
||||
assertNotNull(json);
|
||||
|
||||
assertEquals(expectedJson, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBsonDocument_whenUsingCustomJsonTransformation_thenJsonDateIsStringField() {
|
||||
|
||||
String json = null;
|
||||
try (MongoClient mongoClient = new MongoClient()) {
|
||||
MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
|
||||
Document bson = mongoDatabase.getCollection("Books").find().first();
|
||||
json = bson.toJson(JsonWriterSettings
|
||||
.builder()
|
||||
.dateTimeConverter(new JsonDateTimeConverter())
|
||||
.build());
|
||||
}
|
||||
|
||||
String expectedJson = "{\"_id\": \"isbn\", " +
|
||||
"\"className\": \"com.baeldung.morphia.domain.Book\", " +
|
||||
"\"title\": \"title\", " +
|
||||
"\"author\": \"author\", " +
|
||||
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
|
||||
"\"name\": \"publisher\"}, " +
|
||||
"\"price\": 3.95, " +
|
||||
"\"publishDate\": \"2020-01-01T17:13:32Z\"}";
|
||||
|
||||
assertEquals(expectedJson, json);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.bsontojson;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.bson.json.Converter;
|
||||
import org.bson.json.StrictJsonWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class JsonDateTimeConverter implements Converter<Long> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JsonDateTimeConverter.class);
|
||||
static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_INSTANT
|
||||
.withZone(ZoneId.of("UTC"));
|
||||
|
||||
@Override
|
||||
public void convert(Long value, StrictJsonWriter writer) {
|
||||
try {
|
||||
Instant instant = new Date(value).toInstant();
|
||||
String s = DATE_TIME_FORMATTER.format(instant);
|
||||
writer.writeString(s);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(String.format("Fail to convert offset %d to JSON date", value), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
<groupId>com.baeldung.examples.r2dbc</groupId>
|
||||
<artifactId>r2dbc-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>r2dbc-example</name>
|
||||
<name>r2dbc</name>
|
||||
<description>Sample R2DBC Project</description>
|
||||
|
||||
<parent>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>io.sirix</groupId>
|
||||
<artifactId>core-api-tutorial</artifactId>
|
||||
<artifactId>sirix</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>core-api-tutorial</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
@@ -43,7 +43,12 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@@ -82,6 +87,23 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mchange</groupId>
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>${c3p0.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-dbcp2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -90,12 +112,30 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<!-- Please note that ojdbc8.jar, ucp.jar and ons.jar dependencies
|
||||
are needed for OracleConfiguration.java, OracleUCPConfiguration.java, SpringOraclePoolingApplicationOracleLiveTest.java
|
||||
and SpringOraclePoolingApplicationOracleUCPLiveTest.java -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java</exclude>
|
||||
<exclude>com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java</exclude>
|
||||
</excludes>
|
||||
<testExcludes>
|
||||
<testExclude>com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleLiveTest.java</testExclude>
|
||||
<testExclude>com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java</testExclude>
|
||||
</testExcludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<jdbi.version>3.9.1</jdbi.version>
|
||||
<spring.boot.dependencies>2.1.8.RELEASE</spring.boot.dependencies>
|
||||
<c3p0.version>0.9.5.2</c3p0.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.spring.oracle.pooling;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@SpringBootApplication
|
||||
@Slf4j
|
||||
public class SpringOraclePoolingApplication implements CommandLineRunner{
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringOraclePoolingApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
log.info("Connection Polling datasource : "+ dataSource);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.spring.oracle.pooling.configuration;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
|
||||
@Configuration
|
||||
@Profile("c3p0")
|
||||
public class C3P0Configuration {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() throws SQLException {
|
||||
ComboPooledDataSource dataSource = new ComboPooledDataSource();
|
||||
dataSource.setUser("books");
|
||||
dataSource.setPassword("books");
|
||||
dataSource.setJdbcUrl("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1");
|
||||
dataSource.setMinPoolSize(5);
|
||||
dataSource.setMaxPoolSize(10);
|
||||
return dataSource;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.spring.oracle.pooling.configuration;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import oracle.jdbc.pool.OracleDataSource;
|
||||
|
||||
@Configuration
|
||||
@Profile("oracle")
|
||||
public class OracleConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() throws SQLException {
|
||||
OracleDataSource dataSource = new OracleDataSource();
|
||||
dataSource.setUser("books");
|
||||
dataSource.setPassword("books");
|
||||
dataSource.setURL("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1");
|
||||
dataSource.setFastConnectionFailoverEnabled(true);
|
||||
dataSource.setImplicitCachingEnabled(true);
|
||||
// Only with clients prior to v11.2
|
||||
// dataSource.setConnectionCachingEnabled(true);
|
||||
return dataSource;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.spring.oracle.pooling.configuration;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import oracle.ucp.jdbc.PoolDataSource;
|
||||
import oracle.ucp.jdbc.PoolDataSourceFactory;
|
||||
|
||||
@Configuration
|
||||
@Profile("oracle-ucp")
|
||||
public class OracleUCPConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() throws SQLException {
|
||||
PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource();
|
||||
dataSource.setUser("books");
|
||||
dataSource.setPassword("books");
|
||||
dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
|
||||
dataSource.setURL("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1");
|
||||
|
||||
dataSource.setFastConnectionFailoverEnabled(true);
|
||||
dataSource.setInitialPoolSize(5);
|
||||
dataSource.setMinPoolSize(5);
|
||||
dataSource.setMaxPoolSize(10);
|
||||
return dataSource;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.spring.oracle.pooling.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.spring.oracle.pooling.entity.Book;
|
||||
import com.baeldung.spring.oracle.pooling.exception.BookstoreException;
|
||||
import com.baeldung.spring.oracle.pooling.repository.BookRepository;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/books")
|
||||
public class BookstoreController {
|
||||
|
||||
@Autowired
|
||||
private BookRepository repository;
|
||||
|
||||
@GetMapping(value = "/hello")
|
||||
public String sayHello() {
|
||||
return "Hello";
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public List<Book> findAll() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
public Book newBook(@RequestBody Book newBook) {
|
||||
return repository.save(newBook);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public Book findOne(@PathVariable Long id) throws BookstoreException {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(BookstoreException::new);
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.spring.oracle.pooling.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [id=" + id + ", name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.spring.oracle.pooling.exception;
|
||||
|
||||
public class BookstoreException extends Exception{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.spring.oracle.pooling.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.spring.oracle.pooling.entity.Book;
|
||||
|
||||
public interface BookRepository extends JpaRepository<Book, Long>{
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
logging.level.root=INFO
|
||||
|
||||
# OracleDB connection settings
|
||||
spring.datasource.url=jdbc:oracle:thin:@//localhost:11521/ORCLPDB1
|
||||
spring.datasource.username=books
|
||||
spring.datasource.password=books
|
||||
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
|
||||
|
||||
# Comment this line for standard Spring Boot default choosing algorithm
|
||||
#spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
|
||||
|
||||
# HikariCP settings
|
||||
spring.datasource.hikari.minimumIdle=5
|
||||
spring.datasource.hikari.maximumPoolSize=20
|
||||
spring.datasource.hikari.idleTimeout=30000
|
||||
spring.datasource.hikari.maxLifetime=2000000
|
||||
spring.datasource.hikari.connectionTimeout=30000
|
||||
spring.datasource.hikari.poolName=HikariPoolBooks
|
||||
|
||||
# Tomcat settings
|
||||
spring.datasource.tomcat.maxActive=15
|
||||
spring.datasource.tomcat.minIdle=5
|
||||
|
||||
# JPA settings
|
||||
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
|
||||
spring.jpa.hibernate.use-new-id-generator-mappings=false
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
@@ -1 +1,9 @@
|
||||
|
||||
# Available profiles
|
||||
# - none for no profile
|
||||
# - oracle-pooling-basic - Oracle database with HikariCP. Loads configuration from application-oracle-pooling-basic.properties
|
||||
# - oracle - Uses OracleDataSource. This profile also needs "oracle-pooling-basic"
|
||||
# - oracle-ucp - Uses PoolDataSource. This profile also needs "oracle-pooling-basic"
|
||||
# - c3p0 - Uses ComboPooledDataSource. This profile also needs "oracle-pooling-basic"
|
||||
spring:
|
||||
profiles:
|
||||
active: none
|
||||
+1
-3
@@ -5,10 +5,8 @@ import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -24,7 +22,7 @@ import com.baeldung.boot.jdbi.service.CarMakerService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@SpringBootTest(classes = {SpringBootJdbiApplication.class, JdbiConfiguration.class})
|
||||
@Slf4j
|
||||
public class SpringBootJdbiApplicationUnitTest {
|
||||
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.spring.oracle.pooling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {SpringOraclePoolingApplication.class})
|
||||
@ActiveProfiles({"oracle-pooling-basic", "c3p0"})
|
||||
public class SpringOraclePoolingApplicationC3P0LiveTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void givenC3p0Configuration_thenBuildsComboPooledDataSource() {
|
||||
assertTrue(dataSource instanceof com.mchange.v2.c3p0.ComboPooledDataSource);
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.spring.oracle.pooling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {SpringOraclePoolingApplication.class})
|
||||
@ActiveProfiles("oracle-pooling-basic")
|
||||
public class SpringOraclePoolingApplicationHikariCPLiveTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void givenHikariCPConfiguration_thenBuildsHikariCP() {
|
||||
assertTrue(dataSource instanceof com.zaxxer.hikari.HikariDataSource);
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.spring.oracle.pooling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {SpringOraclePoolingApplication.class})
|
||||
@ActiveProfiles({"oracle-pooling-basic", "oracle"})
|
||||
public class SpringOraclePoolingApplicationOracleLiveTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void givenOracleConfiguration_thenBuildsOracleDataSource() {
|
||||
assertTrue(dataSource instanceof oracle.jdbc.pool.OracleDataSource);
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.spring.oracle.pooling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {SpringOraclePoolingApplication.class})
|
||||
@ActiveProfiles({"oracle-pooling-basic", "oracle-ucp"})
|
||||
public class SpringOraclePoolingApplicationOracleUCPLiveTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void givenOracleUCPConfiguration_thenBuildsOraclePoolDataSource() {
|
||||
assertTrue(dataSource instanceof oracle.ucp.jdbc.PoolDataSource);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
### Relevant Articles:
|
||||
- [Access the Same In-Memory H2 Database in Multiple Spring Boot Applications](https://www.baeldung.com/spring-boot-access-h2-database-multiple-apps)
|
||||
- [Spring Boot With H2 Database](https://www.baeldung.com/spring-boot-h2-database)
|
||||
- [Hibernate @NotNull vs @Column(nullable = false)](https://www.baeldung.com/hibernate-notnull-vs-nullable)
|
||||
- [Hibernate @NotNull vs @Column(nullable = false)](https://www.baeldung.com/hibernate-notnull-vs-nullable)
|
||||
- [Quick Guide to Hibernate enable_lazy_load_no_trans Property](https://www.baeldung.com/hibernate-lazy-loading-workaround)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
create table PERSON (ID int8 not null, FIRST_NAME varchar(255), LAST_NAME varchar(255), primary key (ID))
|
||||
create table person (id int8 not null, first_name varchar(255), last_name varchar(255), primary key (id))
|
||||
@@ -33,6 +33,11 @@
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user