Issue with spring-boot-starter-jdbc when using in standalone - java

I am trying to use spring-boot-starter-jdbc in a non-web/non-tomcat based scenario.
I tried to exclude tomcat-jdbc so that tomcat doesn't start when i start spring-boot application.
The problem i faced is that
spring.datasource.initialize=false
spring.datasource.url=jdbc:h2:tcp://localhost:52705/~/test
Even though i ask spring-boot to not auto-configure a mem db, it always starts one if i dont include tomcat-jdbc. So, there seems to be no way to use spring-boot-starter-jdbc with non-mem h2 database in a standalone kind of application.
Is there a solution to this problem or is it a bug?

tomcat-jdbc has nothing to do with the Tomcat server. It is just that Tomcat was looking for a better JDBC connection pool library so they made it and used the tomcat name for the library.
If you want to exclude the web component then you really want to remove the spring-boot-starter-web dependency on your project as within that there is the dependency on the spring-boot-starter-tomcat.

Related

how to prevent jdbc from trying to connect to a mysql database during unit testing

I'm making an application for a school project, but I'm running into the issue that when I try to run the unit tests that it tries to connect to the database while starting up the application, which isn't required for the tests (because it will be mocked), and is not available in the CI/CD pipeline.
jdbc connection error
I'm building my project in Java Maven Springboot and would like to know how I can prevent it from trying to connect to the database when running my test.
here is a link to my repository: https://gitlab.com/kwetter_jack/Kwetter_posts/-/tree/ci_cd_setup
Your test classes have #SpringBootTest annotation which will start a Spring application context - as your application uses a database the tests will also try to setup and use a database connection.
The simplest solution is to remove the annotation so the tests no longer try to connect to a database. You'll probably need to mock some more dependencies as a result as Spring is no longer creating these for you. You could also have a look at https://www.baeldung.com/spring-boot-testing for some other ideas how you could alter your tests.
Alternatively if you do want / need the application context to run you can add a application.yaml for the tests that defines and uses a in memory DB so the tests have something to connect to - see https://www.baeldung.com/spring-boot-h2-database for details how to do this.
Just change value under spring.datasource to H2 database to prevent
The application connect the real database.
Test application.yml
FYI, You no need to copy all config from original application.yml, just only some config that you need to override.
while I was investigating the spring boot H2 in-memory database (as suggested by Chris Olive and Paranaaan) I also came across the option of using a test container. after looking into this I saw that this enables the project to create a temp docker container with a MySQL image that I can use during the testing of my project, considering I was planning on using docker anyway for the integration testing of my microservices project I attempted this and it worked as I had hoped it would.
if anyone is interested in the test container solution that I used, the information can be found here:
https://www.testcontainers.org/modules/databases/mysql/

How to update standalone.xml offline in WildFly

As part of my project, I need to support legacy JAAS security domain to protect my EJBs. I am following the quickstart (https://github.com/wildfly/quickstart/tree/master/ejb-security-jaas) and updating the configuration using jboss-cli (https://github.com/wildfly/quickstart/blob/master/ejb-security-jaas/configure-elytron-jaas.cli). Everything is working fine with my POC.
But, I am facing an issue when I am trying to apply this concept in production code. We wrap our production code along with WildFly and ask customer to start our product (which internally starts WildFly). As per my understanding, jboss-cli needs WildFly to be running- so, I am trying with the following two approaches
Approach 1
Start WildFly
Run jboss-cli and make all the config related to supporting legacy JAAS security domain
Restart WildFly
This is having lots of challenges including the requirement to restart
Approach 2
Update the standalone.xml (using ant script during my product build time)
Package my production code along with updated standalone.xml and WildFly
Currently, I am following approach 2, but updating standalone.xml with ant script looks inefficient to me. Is there any better approach? Any suggestion from the experts is welcome.
You can do what you want using embed-server. Below is a script I use to add a datasource using jboss-cli. The key parts of this are the embed-server and batch parts:
embed-server --server-config=standalone.xml --std-out=echo
batch
module add --name=org.postgres --resources=${user.home}/Downloads/postgresql-42.2.12.jar --dependencies=javax.api,javax.transaction.api
/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)
/subsystem=datasources/data-source=blah/:add(connection-url=jdbc:postgresql://localhost:5432/blah,driver-name=postgres,jndi-name=java:/jdbc/blah,initial-pool-size=4,max-pool-size=64,min-pool-size=4,password=blah,user-name=blah)
run-batch
You can see that this is updating standalone.xml. Obviously if you are using a different configuration file you can use it here.
An important part of this is that Wildfly should not be running.

Is it possible to make a Spring MVC web app run as a "standalone executable" with Java and Tomcat embedded?

I have a web app built with Java, Spring MVC, and JDBC. The result is a WAR file.
To run it, the user has to install Java 8 JDK and Tomcat, and deploy the WAR file to the Tomcat server.
It would be great if they could just download the one file run it as a standalone application.
That is, run "the WAR file" and just browse to http://localhost:8080/myapp
Also, on Windows it would be great it was setup as a Server (like Tomcat is when installed with the installer).
Is there any way to do this? Maybe with Spring Boot or something new like that?
Yep, Spring boot is the way to go.
It allows you to build an executable Jar with all dependencies and a Tomcat (by default, can be changed) embedded.
But users will still need to download a JRE to execute the Jar, and a database if it's required, but you can use en embedded database like H2, HSQLDB..., depends what is your needs.
Yes . you can use spring boot to achieve your results. Kindly refer the below link for sample code
https://mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/
You can use embedded jetty server using maven but that would require you to setup few things your app and may have align your existing app, please check this article for more information.
Jetty is similar to tomcat server in terms of running spring application, there are not much difference in terms of development. Tomcat is just more famous.
Other option as others said, is to migrate your app to spring boot which would be easy if you already have app written in spring (But that depends how much code you have and how much time you have)

Re-deploy spring boot service without restart?

I have developed a micro service (Spring Boot REST service, deployed as executable JAR) to track all activities from third party projects as my requirement and its working now.
Currently it's working apart of some projects, and now I have updated service with some additional features.
But I can't move it to live server without restarting the existing service as it is deployed as jar. I'm afraid to restart my service, restart may be leads to lose data of integrated projects.
What improvements can I make in my architecture to solve my problem?
What about JRebel plugin. It worked perfectly for me, but, unfortunately, it's not a free app. Like alternative, (i used this approach with Spring MVC, with Spring Boot it could be otherwise), I set up a soft link in work directory on a compiled path in JBoss (in my case it was dir with name target and *.class and *.jar files). As for me, the first solution with JRebel is the most appropriate for you.
Finally got a solution as commented by #Gimby .
We can do it by deploying multiple instances of services and it bound to a service registry ,Here i achieved it by using eureka as registry service and also used zuul as proxy .

Weblogic Embedded Spring

I read in a forum that Weblogic comes with a version of Spring inside it, but I have been searching how to use that and I didn't find a way.
Is it true?
Is there a way to deploy a Spring application in Weblogic without put the jars related to Spring inside the application or in Weblogic's lib directory?
Thanks.

Categories

Resources