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.
Related
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)
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.
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.
I have a Play Framework 2.2.2 application that I am deploying as a .war file and running under Tomcat 7. My application runs for days without problems on my local dev machine (through Play's built in server, not Tomcat), but once I deploy it under Tomcat, after several hours the Tomcat server will lock up, taking down all the other applications running on it as well.
I think the problem is that the BoneCP connection pool in Play, and the built-in connection pool of Tomcat are conflicting. There isn't much or any useful information in the Tomcat logs, so I'm kind of left guessing here.
I'd like to disable the BoneCP connection pooling within my Play application, but cannot find any information on how to do so.
Any advice appreciated!
There are several possible solutions for this, which might be more or less preferrable for your deployment environment.
Play gives you an "out-of-the-box" database connection, which you don't need to use. Drop the Play JDBC component from your build file (remove jdbc from your libraryDependencies) and setup your JDBC connections manually by yourself. For example, you can make a singleton TomcatConnectionPool that has a function getConnection() that gives you the JDBC connection you need for use in your Play actions.
Write your own plugin specifically extending Play's DBPlugin interface so that it's a database plugin. Implement it like Play's BoneCPPlugin but make it use the Tomcat connection pool instead of BoneCP.
Use someone else's already made custom Play Database Plugin, like this one that uses c3p0. I have some anecdotal evidence that c3p0 works well with Tomcat, but your mileage my vary.
I try to use Glassfish/MySQL. I have created JDBC resource and JDBC connection pool for MySQL.
But if I tried to put MySQL JDBC resource in jta-data-source, nothing works.
Then if I tried to modify jdbc/__default and change its connection pool from DerbyPool to MySQL, it works. My entity gets persists to the correct table.
So do I have to use jdbc/__default only as my JDBC resource for my app? How can I use the JDBC resource and JDBC connection pool I created in my app?
I really have hard time understanding how to use JDBC in Glassfish.
This is my first time to ask question in this forum. Thank you very much.
See this link for a step by step tutorial on how to create JDBC connection pools in Glassfish server. Here is the official documentation on how to do it. Or you can read this SO question. And another resource that you can use is this SO question.