BAEL-3075 - Initial commit

This commit is contained in:
Aitor Cuesta
2020-04-05 12:51:27 +02:00
parent ae8fb70e40
commit 6d60aad5f9
17 changed files with 424 additions and 8 deletions
@@ -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);
}
}
@@ -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;
}
}
@@ -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 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);
return dataSource;
}
}
@@ -0,0 +1,30 @@
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.setMaxPoolSize(10);
return dataSource;
}
}
@@ -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);
}
}
@@ -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 + "]";
}
}
@@ -0,0 +1,10 @@
package com.baeldung.spring.oracle.pooling.exception;
public class BookstoreException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
}
@@ -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>{
}