Configuring embedded Derby in Spring Boot app - spring-boot

Could you please help me set up connection to the embedded Derby database in Spring Boot application?
I searched the web but can only find solutions for server-type Derby, not for embedded Derby.
spring.jpa.database = ?
spring.jpa.hibernate.ddl-auto = create-drop

Derby as an in-memory database
If you want to Configure in-memory Derby database with spring boot, the minimal thing you need is to mention the runtime dependency (along with spring-boot-starter-data-jpa) as
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
This bare minimum configuration should work for any embedded datasource in Spring Boot. But as of current Spring Boot version (2.0.4.RELEASE) this lead to an error only for Derby
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing
DDL via JDBC Statement
Caused by: java.sql.SQLSyntaxErrorException: Schema 'SA' does not exist
This happens because of default configuration ofspring.jpa.hibernate.ddl-auto=create-drop see this spring-boot issue for details.
So you need to override that property in your application.properties as
spring.jpa.hibernate.ddl-auto=update
Derby as a persistent database
If you want to use Derby as your persistent database. Add these application properties
spring.datasource.url=jdbc:derby:mydb;create=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect
spring.jpa.hibernate.ddl-auto=update

You don't need connection properties if you are using Spring Boot. Just add these to your POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
</dependency>
Then add the usual controller, service, and repository classes.

Related

Tracing SQL Queries with X-Ray and Spring Boot 2

The current X-Ray SQL tracing interceptor uses Tomcat JDBC Pool but Spring Boot 2 uses HikariCP as default pool, is it possible to configure the jdbc tracing in HikariCP instead?
Here (https://forums.aws.amazon.com/thread.jspa?threadID=254847) they suggest to use both Datasources:
DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
HikariDataSource hikariDataSource = new HikariDataSource();
... // data source configuration
dataSource.setJdbcInterceptors("com.amazonaws.xray.sql.postgres.TracingInterceptor;");
hikariDataSource.setDataSource(dataSource);
But if I have the HikariCP library in the classpath spring will configure that as datasource.
I've tried with a DatasourceBuilder and also forcing the type using the parameter spring.datasource.type
Any hint?
In Spring boot , you can use still use Tomcat over HikariCP as connection pool:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
We are currently investigating a solution that will work with both Tomcat JDBC and HikariCP. We are aware that there are currently no work arounds without having Tomcat JDBC as a dependency. Please stay tuned.
Resolved the same using TracingDataSource, while still using HikariCP as the connection pool
Reference https://github.com/aws/aws-xray-sdk-java/issues/88#issuecomment-570328275
Code: (Note, I am using AWS Secrets Manager JDBC Library aws-secretsmanager-jdbc to connect to the database using secrets stored in AWS Secrets Manager)
import com.amazonaws.xray.sql.TracingDataSource;
...
...
#Bean
#ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return TracingDataSource
.decorate(DataSourceBuilder.create()
.driverClassName("com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver")
.url("jdbc-secretsmanager:postgresql://" + System.getenv("PGHOST") + ":"
+ System.getenv("PGPORT") + "/" + System.getenv("PGDATABASE"))
.username(System.getenv("SECRET_NAME")).build());
}
Dependency:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-sql</artifactId>
</dependency>

How to replace default hikari cp to tomcat pool on spring boot 2.0

I have migrated spring boot application to 2.0 and found out some problems with hikari connection pool. When I am fetching database data this results to hikari cp timeout ie. connection is not available. I don't know why when in the previous version this worked correctly.
Therefore I tried to use tomcat pool with this config in application.yml but it did not work (in correct YAML formatting).
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
My pom.xml has these dependencies related to DB things:
spring-boot-jpa
spring-boot-jdbc
jdbc7
How to exclude hikari and use tomcat connection pool?
I have found out the solution.
This can be resolved in pom.xml by modifying like that:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
However the hikari problem was probably with default small size of connection pool. So this problem could be resolved also with this change but not verified by myself. Just note for others. Something like that:
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=5
Since Spring Boot 2.0 release, spring-boot-starter-jdbc and spring-boot-starter-data-jpa resolve HikariCP dependency by default and spring.datasource.type property has HikariDataSource as default value.So if u have both dependency in your application you should exclude it from both like below.
implementation('org.springframework.boot:spring-boot-starter-data-jpa') {
exclude group: 'com.zaxxer', module: 'HikariCP'
}
implementation('org.springframework.boot:spring-boot-starter-jdbc') {
exclude group: 'com.zaxxer', module: 'HikariCP'
}
After that you can configure other pooling technologies that u likes to use, like below
.
In your application.yml file :
spring:
datasource:
type: org.apache.tomcat.jdbc.pool.DataSource
In dependency :
implementation('org.apache.tomcat:tomcat-jdbc')
Also:
spring:
datasource:
type: org.apache.tomcat.jdbc.pool.DataSource
works with
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>

Spring boot failing to start with Spring Cloud AWS Core dependency

I am writing a spring boot app that accesses stuff from an s3 bucket, but I get a NoClassDefFoundError when I use the starter spring-cloud-starter-aws dependency from the spring initializer.
Am I missing some other dependency here?
Below are my dependencies.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I also defined the dependencyManagement block for spring-cloud-dependencies and use Edgware.SR1 as my spring-cloud-version.
My app fails with the following error when starting up.
2018-01-24 12:20:25.642 INFO 1980 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-24 12:20:25.666 ERROR 1980 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.art.productattribution.consumerintegration.ConsumerIntegrationApplication]; nested exception is java.lang.NoClassDefFoundError: com/amazonaws/AmazonClientException
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:616) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
Not sure what am I missing here? Please let me know if you need any more details with this. The version of spring boot I am using is 1.5.9.RELEASE
The correct dependency is the spring-cloud-aws-context. Add the following in your pom file (version 1.2.2 as of Nov 22, 2017):
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-aws-context -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-context</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
Below are the Spring Cloud AWS modules:
Spring Cloud AWS Core is the core module of Spring Cloud AWS providing basic services for security and configuration setup. Developers will not use this module directly but rather through other modules. The core module provides support for cloud based environment configurations providing direct access to the instance based EC2 metadata and the overall application stack specific CloudFormation metadata.
Spring Cloud AWS Context delivers access to the Simple Storage Service via the Spring resource loader abstraction. Moreover developers can send e-mails using the Simple E-Mail Service and the Spring mail abstraction. Further the developers can introduce declarative caching using the Spring caching support and the ElastiCache caching service.
Spring Cloud AWS JDBC provides automatic datasource lookup and configuration for the Relational Database Service which can be used with JDBC or any other support data access technology by Spring.
Spring Cloud AWS Messaging enables developers to receive and send messages with the Simple Queueing Service for point-to-point communication. Publish-subscribe messaging is supported with the integration of the Simple Notification Service.
Ref: http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_using_amazon_web_services
It seems com.amazonaws.AmazonClientException is not found in the classpath. I think you can add the following dependency in your POM.xml file to solve this issue.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-core</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
You need to include dependency mentioned by #alltej. Also you need to add below property in application.properties file if you are running on local.
cloud.aws.stack.auto=false

Why do my database customizations not get applied when using Spring Data JPA with Spring Boot?

I'm trying to use Spring Data JPA in a Spring Boot project in this tutorial. These are my pom.xml dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
and application.properties
# DataSource settings: set here configurations for the database connection
spring.datasource.url = jdbc:mysql://localhost/dao
spring.datasource.username = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.password =
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate settings are prefixed with spring.jpa.hibernate.*
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
and I get this error:
Caused by: org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:137)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiat e(SimpleInstantiationStrategy.java:162)
... 40 more
Is there a problem in datasource configuration?
The error is thrown in DataSourceProperties.getDriverClassName() method. Find below the source code of the same from spring distribution:
if (!StringUtils.hasText(driverClassName)) {
throw new BeanCreationException(
"Cannot determine embedded database driver class for database type "
+ this.embeddedDatabaseConnection
+ ". If you want an embedded "
+ "database please put a supported one on the classpath.");
}
Spring throws this error when spring.datasource.driverClassName property is empty. So to fix this error, make sure that the application.properties is in the classpath.
I ran into that same error and my problem was that the application.properties was not packaged in the
jar and I wasn't providing it on startup.
If you are starting up using java -jar you-jar-name.jar make sure that the application.properties is available. Per the docs:
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

Session factory issue in setting up hibernate distributed ehcache with terracotta

I am trying to setup EHCache in distributed environment with Terracotta. Here I am able to connect Application server and Terracotta server and in terracotta Developer Console I am able to see replicated objects.
But in application server continiously following exception message is appearing, though rest of the application is running properly:
Hi All, if any body can guide why this exception message appears and how we can resolve it.
Also it would be helpful for me have any comprehensive tutorial for setting up terracotta for hibernate application.
Here [CacheByAmitNode8081] is the name of cache node I have defined in application server.
[DEBUG][04/05/12 12:49:07.973][CacheByAmitNode8081] Running mbean initializer task for ehcache hibernate...
[DEBUG][04/05/12 12:49:07.995][CacheByAmitNode8081] Successfully registered bean
[ERROR][04/05/12 12:49:08.000][CacheByAmitNode8081] Error locating Hibernate Session Factory
java.lang.NullPointerException
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:36)
at sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.get(UnsafeQualifiedObjectFieldAccessorImpl.java:20)
at java.lang.reflect.Field.get(Field.java:358)
at org.hibernate.cache.ehcache.management.impl.ProviderMBeanRegistrationHelper$RegisterMBeansTask.locateSessionFactory(ProviderMBeanRegistrationHelper.java:152)
at org.hibernate.cache.ehcache.management.impl.ProviderMBeanRegistrationHelper$RegisterMBeansTask.run(ProviderMBeanRegistrationHelper.java:117)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
[DEBUG][04/05/12 12:49:08.001][CacheByAmitNode8081] SessionFactory is probably still being initialized... waiting for it to complete before enabling hibernate statistics monitoring via JMX
[DEBUG][04/05/12 12:49:08.001][CacheByAmitNode8081] Running mbean initializer task for ehcache hibernate...
[ERROR][04/05/12 12:49:08.001][CacheByAmitNode8081] Error locating Hibernate Session Factory
java.lang.NullPointerException
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:36)
at sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.get(UnsafeQualifiedObjectFieldAccessorImpl.java:20)
at java.lang.reflect.Field.get(Field.java:358)
at org.hibernate.cache.ehcache.management.impl.ProviderMBeanRegistrationHelper$RegisterMBeansTask.locateSessionFactory(ProviderMBeanRegistrationHelper.java:152)
at org.hibernate.cache.ehcache.management.impl.ProviderMBeanRegistrationHelper$RegisterMBeansTask.run(ProviderMBeanRegistrationHelper.java:117)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)*
When you using Terracotta, you need to add specific terracotta jars on your classpath that will replace ehcache implementation. In your exception stack I don't see any terracotta classes output...
In my maven dependencies I have:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core-ee</artifactId>
<version>2.5.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-terracotta-ee</artifactId>
<version>2.5.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.terracotta</groupId>
<artifactId>terracotta-toolkit-1.5-runtime-ee</artifactId>
<version>4.2.0</version>
<scope>compile</scope>
</dependency>
You can also check Terracotta tutorial on how to plug to Hibernate:
http://terracotta.org/documentation/enterprise-ehcache/get-started-hibernate

Resources