Good day!
I have a simple springboot application with mysql jdbc repository.
I have properties for connect to DB
spring.datasource.url=jdbc:mysql://*:3306/*?useSSL=false
spring.datasource.username=*
spring.datasource.password=*
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.initialize=true
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.max-total=1
My test DB has only max 10 connections for user. When I use console
SHOW STATUS WHERE variable_name = 'threads_connected';
I can see that now DB has 5 connections only but when I try to start my application I get Exception
2018-02-28 10:26:24.115 ERROR 17360 --- [nio-8080-exec-3]
o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial
connections of pool.
java.sql.SQLSyntaxErrorException: User '*' has exceeded the
'max_user_connections' resource (current value: 10)
How can I fix it? And why I get that Exception if I have 5 free connetion on DB and I need only 1 connection for pool from properties? I can't edit max connection on DB because use Heroku like testDB. I can edit only tomcat properties only
You configured the following property:
spring.datasource.dbcp2.max-total=1
This indicates that you're trying to use the DBCP 2 connection pool. However, when you check the stacktrace, you can see the following:
o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
As the package of the ConnectionPool class is org.apache.tomcat, this indicates that you're actually using the default Tomcat connection pool. This means that your max-total poperty is not being picked up properly.
If you want to configure this for a Tomcat connection pool, you need to use the maxActive property:
spring.datasource.tomcat.max-active=1
Alternatively, if you don't want to use the Tomcat connection pool, you can add the DBCP 2 dependency using Maven/Gradle/... . If you exclude the default Tomcat connection pool, it will automatically pick up DBCP 2.
Another possibility is to configure it by using the spring.datasource.type property as mentioned by the documentation:
You can bypass that algorithm completely and specify the connection pool to use via the spring.datasource.type property. This is especially important if you are running your application in a Tomcat container as tomcat-jdbc is provided by default.
For example:
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
I was stuck in similar situation but the simple fix for this could be like the database that you are pointing in application.properties probably does not exist.
Or You can try to delete org.springframework.data folder from C:\Users\{yourusername}\.m2 folder and update Maven project.
I got exactly same error and It worked for me after changing the spring boot version from 1.5.* to 2.1.8.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Same problem here... try to change the version on parent like says SaravanaKumar but the project is to old and I get a lot of deprecated.
I Fixed by change only the database version
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version> <!--Add that Line-->
<scope>runtime</scope>
</dependency>
Note: if update on parent all dependency get update.
Related
I am implementing a program in Spring-Boot using ObjectDB. To actually use ObjectDB I have followed this approach which is working perfectly.
However, as soon as I want to use `spring-boot-starter-web`` then I am getting the following errors:
dataSource or dataSourceClassName or jdbcUrl is required. at
com.zaxxer.hikari.HikariConfig.validate
I have been fiddling around with parameter jdbc-url in the properties file as mentioned in many posts. Tried to exclude Hikari because probably ObjectDB uses his own connection pooling mechanism. But without any results.
Any ideas on how to solve this error?
I am using the exact same code as in the link. I have added Spring-Actuator in the pom like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
In some cases, frameworks that use JPA require specifying a JDBC connection details, including a JDBC driver, which is then passed to the JPA implementation and used by it to access the database. ObjectDB is a JPA implementation that does not access an external database, and therefore, does not need or use a JDBC driver.
As discussed in the comments to the question, a simple workaround is to specify a dummy JDBC driver, which will be passed to ObjectDB and then ignored. It does look weird, but this is the way to go until either ObjectDB implement its own JDBC driver, or the relevant frameworks become more flexible regarding their request of a JDBC driver.
Have you tried this (in the pom.xml?):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.objectdb</groupId>
<artifactId>objectdb</artifactId>
<version>2.8.4</version>
</dependency>
And don't forget to add the database connection in application.properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=mydbuser
spring.datasource.password=mydbpass
spring.datasource.url=jdbc:....
See https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa for more details.
I'm trying to run my spring boot applicationon localhost ,normally i should get Whitelabel error page for the first time,but i got this error while running it.
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
As shown in your log, you try to run the application without providing information about the database.
Spring and Spring-boot are not entirely based on magic.
They can guess some database information, like the url if you use an embedded datasource (as mentioned in the log you provided). Have this dependency on your classpath if you want to run your application with an in-memory database:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
Make sure there is not the <scope>test</scope> in this dependency. And, at runtime, spring-boot will automatically connect to your hsqldb database.
If you are running your application and you have a production oriented database, like PostgreSQL for instance, then spring-boot can't guess what is the connection information, like the url or the database name. And you have to provide this properties in the application.properties file:
spring.datasource.url=jdbc:postgresql://<database_host>:<port>/<database_name>
spring.datasource.username=myUser
spring.datasource.password=secret
spring.datasource.type= (Not necessary)
If you don't provide that, it's like posting a mail without giving any adresses... you can't find the person you are looking for.
Hope it helps !
As the log depicts, you have to review your data-source configs, and make sure all good.
I have a spring boot REST API application for a project with 2 different setups ( =2 different jars). Both need to be realized, but I have no idea on how to do it or what the best way would be.
Both setups need to have a connection with an online database (on a server; AWS) and a connection with an offline or local database (running on the machine/pc itself). It is the offline or local connection that is different between both setups.
Setup 1:
When the application is started it needs to connect to the online database. When an error occurs or connection to the online database is lost, it needs to connect to the offline/local database.
It is not required that that it will reconnect with the online database after an error occurred or the connection was lost.
Setup 2:
When the application is started it needs to connect to both the online and the offline database. So when the user does a post to the REST API, both the online and the offline database need to be updated (unless an error occurs or the connection to the online database is lost). If the user just does a get request, it is preferred to get the data from the online database, unless an error occurs or the connection to the online database is lost, than it can use the offline database.
It is not required that that it will reconnect with the online database after an error occurred or the connection was lost, but in this setup it would be nice.
Synchronizing the data after connection has been established again isn't required either (but would maybe be a nice feature).
I have seen a similar post where the solution was to use ha-jdbc but it is an old post... Maven just doesn't find the dependency when I try adding it to my pom.xml file.
After some more searching and trying I was able to add ha-jdbc to my pom.xml. What I had to do was add a repository that contained ha-jdbc.
<project>
<repositories>
<repository>
<id>jbossrepository</id>
<name>jbossrepository</name>
<url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url>
</repository>
</repositories>
<dependency>
<dependency>
<groupId>net.sf.ha-jdbc</groupId>
<artifactId>ha-jdbc</artifactId>
<version>3.0.3</version>
</dependency>
</dependency>
</project>
Yet to see if I can get it to work and if it is what I am looking for...
ha-jdbc isn't worth trying, one error or problem after the other, it's just old java too...
Versions:
- java: 1.8
- org.springframework.boot (spring-boot-starter-parent): 2.1.8.RELEASE
- Database (online & offline): PostgreSQL 11.5
Take a look this example code. There you can find already implemented a working solution.
The logic behind it is EnableJpaRepositories which enables it based on packages (basePackages)
The case is that I have launched both Spring Cloud Data Flow Server and Shell apps in my local machine, and they all used the H2 memory database to store the tasks and jobs defination.
When I deployed the application that Used a H2 as the database to read data, it works perfect fine.
But when I want to deploy and run the application that read data from a local postgresql database, it just could not find it, and will then use the H2 again.
I have add postgresql dependency in my pom and also configure the properties in the application.properties , it works fine with spring batch in my local Intelij.
So my question is that how could I make my application still read data from the postgresql after it been deployed in the spring-cloud-data-flow-server? Thanks.
Configuration info:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version
<spring.cloud.task.version>2.0.0.RELEASE</spring.cloud.task.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-core</artifactId>
<version>${spring.cloud.task.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-batch</artifactId>
<version>${spring.cloud.task.version}</version>
</dependency>
You can control what datasource should be configured to SCDF, Tasks, Task-launcher, and even Composed Task Runner. The documentation for these options could be useful.
In your case, though, it sounds like you have 2 different databases: one for SCDF and the other for the Task that SCDF launches, which should be a pretty straightforward setup as long as the datasource properties are configured correctly on both the Boot apps.
It could be that either there's no Postgres driver in the classpath or the datasource properties aren't configured correctly. Review the docs of the connection properties for the supported databases - you could double check for correctness.
Just a quick question but I have been struggling for a while with this now - any help would be appreciated. I have just managed to get my first jetty instance running by setting it up using maven. Now it runs successfully using mvn jetty:run.
However, I need to have the jetty instance connect to a mysql database. How can I do this? all online documentation I have read refers to a start.ini file but I am doing everything through maven so am not sure how this applies. Has anyone got a simple list of instructions of what I would need to do to have my app connect to mysql? Preferably using a connection pool.
Thanks for any help you can offer
If you don't need to use a connection pool so I just recommend you write the code to connect it to MySQL.
first, you will need to import mysql-connector from maven
Add at this few lines to your pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
Then if you have created a class that manages the connection to your database. And you don't care much about pooling your connections then
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://{IP_TO_YOUR_SERVER}:{PORT[DEFAULT=3306]}/{DB_NAME}?user={USERNAME}&password={PASSWORD});
However, if you want to use a connection pool you probably will need to check "Jetty/Howto/Configure JNDI Datasource"
And I think this link to stack overflow can help as well.
JNDI lookup failing with Jetty for JDBC connection pooling with MySQL?
I have been struggling all morning with the same issue and to be honest I tried the first approach. And this moment I am using that solution.