How to select a database in run time using spring boot - database

I have created an application with 2 databases. I need to select one of them in run time. Is there any method or class in spring boot.

You can do it using application.properties configurations. You don't need to restart the server for those configurations to act.
Please refer to:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Drive
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

Related

H2 database "if" query grammar [duplicate]

I have some problems with using a schema.sql file to create my sql schema when executing a junit test while this schema contains mysql specific expression. I have to add the mode=mysql to the H2 url.
For example something like this:
jdbc:h2:mem:testd;MODE=MYSQL
But Spring boot automatically uses the url defined in the enum
org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection with its url
jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE.
I have tried similiar approaches to get this to work, but spring does not take the spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL from my test-application.properties. All other settings from my test-application.properties have been read successfully.
If I let spring/hibernate create the schema (without the schema.sql file) with the javax.persistence annotations in my entities everything works fine.
Is there a simple way to add a mode?
Set
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
in application-test.properties, plus
#RunWith(SpringRunner.class)
#DataJpaTest
#AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
#ActiveProfiles("test")
on the test class
I was having this same issue. It would not pick up the url when running tests. I'm using flyway to manage my scripts. I was able to get all of these working together by following these few steps.
Created a V1_init.sql script in src/test/resources/db/migration so that it is the first script run by flyway.
SET MODE MYSQL; /* another h2 way to set mode */
CREATE SCHEMA IF NOT EXISTS "public"; /* required due to issue with flyway --> https://stackoverflow.com/a/19115417/1224584*/
Updated application-test.yaml to include the schema name public:
flyway:
schemas: public
Ensure the test specified the profile: #ActiveProfiles("test")
I have tried similiar approaches to get this to work, but spring does not take the spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL from my test-application.properties
Did you try to append this parameters instead of rewriting the existing ones?
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
All other settings from my test-application.properties have been read successfully.
I thought that file should be named application-test.properties.
I was able to run it with this config:
# for integration tests use H2 in MySQL mode
spring.datasource.url=jdbc:h2:mem:testdb;DATABASE_TO_LOWER=TRUE;MODE=MySQL;
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
The main trick here is to force Hibernate to generate SQL scripts for MariaDB dialect because otherwise Hibernate tries to use H2 dialect while H2 is already waiting for MySQL like commands.
Also I tried to use more fresh MariaDB103Dialect for MariaDB 10.3 but it doesn't worked properly.
You need to set MYSQL mode on h2 and disable replacing of datasource url for embedded database:
Modify application-test.yaml
spring:
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=MYSQL
test:
database:
replace: NONE

How to test Spring database down?

I have this SpringBoot server app using PostgreSQL database if it's up and sending error response if it's down. So my app is running regardless the database connection.
I would very much like to test it (jUnit / mockmvc).
My question is very simple, yet I did not find the answer online:
how does one simulate a database connection loss in SpringBoot?
If anyone wants, I can supply code (project is up at https://github.com/k-wasilewski/workshop/)
Have you thought of Testcontainers? You can spin up your docker image through a Junit test and make your spring boot use that as your database.
Since you use junit, you can start/stop this container at will.
This will generate a test which creates the condition you are looking for and write code as to what to expect when the database is down.
Here are some links to get started,
Testcontainers and Junit4 with Testcontainers quickstart - https://www.testcontainers.org/quickstart/junit_4_quickstart/
Spring boot documentation - Use Testcontainers for integration testing
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-testcontainers
Testcontainer github link example for springboot app
https://github.com/testcontainers/testcontainers-java/tree/master/examples/spring-boot
Testcontainer - Generic container javadoc. You can find methods for start/stop
container here. call from your Junit.
https://javadoc.io/static/org.testcontainers/testcontainers/1.12.4/org/testcontainers/containers/GenericContainer.html
You can implement your own Datasource based on DelegatingDataSource and then let it throw exceptions instead of delegating when ever you want to.
I've done this before by creating a Spring Boot test configuration class that created the DataSource and wrapped it in a Java proxy. The proxy simply passed method calls down to the underlying DataSource, until a certain flag was set. Once the flag was set, then any method called on the proxy would throw an exception without calling the underlying DataSource. Essentially, this allowed me to "bring the database down" or "up" simply by flipping the flag.

JNDI datasource and ApplicationModule tester

I have an ADF application module and I have it configured to use a JNDI datasource (e.g., JNDI name = jdbc/ORCL).
That all works fine, but when I try to test my Application Module through the integrated tester in JDeveloper (i.e., run the application module directly instead of running a page), it cannot find the JNDI data source, even if my integrated Weblogic Server is started.
Is this just a drawback of using JNDI: that you cannot use the integrated application module tester anymore? Or is there a way to configure JDeveloper to make this all work together (JNDI + integrated AM tester)?
You should make use of the default JDBC url, i.e. java:comp/env/jdbc/ORCL instead of using the short form jdbc/ORCL. Many features of JDeveloper rely on the full url to work. For example, even if you don't manually create the datasource on your Integrated WebLogic Server, you still can right-click and run a page to test because behind the scene, JDeveloper automatically create a temporary datasource with that name to run the application.
When you deploy your application to an actual standalone server, the application will automatically trim the java:comp/env/ portion of the full url and look for the datasource with the name jdbc/ORCL. You don't need to shorten it yourself :).

Implementing EJB timer service on a datasource in an application server

I am migrating my application from JBoss 6 AS to Wildfly 8.2.0. AS. As a part of it, I need to migrate EJB timer service defined on the configured datasource in JBoss 6 AS.
I have gone through the EJB timer service study material and got that it is used to do a job on a schedule basis.
Implementing EJB timer service in datasource in an application server - what does this mean ? In wildfly AS, there is a file timer-sql.properties which requires a syntax change according to the database employed and an entry in standalone-full-ha.xml as mentioned below.
timer-service thread-pool-name="timer" default-data-store="clustered-store">
<data-stores>
<database-data-store name="clustered-store" datasource-jndi-name="java:/TestDB" partition="timer"/>
</data-stores>
</timer-service>
I would like to understand more on the same and hence, can anyone please explain what the EJB timer service configured on a datasource in an application server does and how it works at a high level. Also, please suggest any link that explains the same. Thanks.

Spring - how to load configuration from database?

I need to load configuration settings used in applicationContext.xml from relational database (PostgreSQL).
I followed this article http://www.codeproject.com/Articles/28893/Loading-Application-Properties-from-a-Database but it uses a deprecated "Spring Modules" jar file.
Is there some another technique how to achieve it? I use Spring 3.
My idea is to set only access setting to database (hostname, dbname, username, password) for creating a datasource and probably create some own handler class(?) for loading setting from DB. In applicationContext.xml I will use settings in the same way - ${foo.bar}.
Plesase share your experience and code examples with this topic and Spring 3.
Although it's no longer bundled in Spring 3 I don't see any reason why you can't use it. Just include
<dependency>
<groupId>org.springmodules</groupId>
<artifactId>spring-modules-jakarta-commons</artifactId>
<version>0.8</version>
</dependency>
On your maven pom alongside with other Spring 3 dependencies and the setup as described in the tutorial should look alright.
If that produces specific error please post the details (incl. stack trace) so we can help investigate further

Resources