I am trying to understand the correlation between Database connections and spring boot app. My spring boot app is connecting to one schema and I am running 4 such spring boot apps on my system. Each app is connecting to a different schema.
The problem is that these 4 apps are acquiring 50 oracle db connections, but when I close all apps and open DB through oracle sql developer only one connection is acquired.
I don't have enough rep to post this as a comment.
By default Spring Boot uses HikariCP as a connection pooling framework. Some good information can be found at Baeldung, which I recommend using as it covers lots Spring Boot functionality and is almost always up to date.
https://www.baeldung.com/hikaricp
Spring-boot specific information:
https://www.baeldung.com/spring-boot-hikari
While 10 connections are not at all required, you can 'play' around with how much is best (or most optimum) for your app and set it as best fits your design. Usually this will be done by optimizing this as the need arises in your application. 10 is what spring/hikari identifies as a good starting point for most projects.
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#data.sql.datasource.connection-pool
Since I had to post this as an answer I'll go a little more in depth in your actual question:
If we think of a connection to a database without a pool we can think of:
The application requests the driver to open a connection to your database
A socket is opened between your application and the database
You are authenticated to the database
Your query runs and the connection is closed
This is fine in a small application without many requests going through, but hopefully you can see the issue here as we scale and get more users. Removing the first 3 steps can make a huge difference.
It should also be noted that while your database holds 3 connections, the connections will be idle and not generating much (if any) load on your database.
Related
I have a spring-boot application that I'm deploying to an Azure App Service. This application connects to a MySQL server. I would like that the application would retry the database connection. I looked around for a solution and I found this blog post.
My question: is there any spring setting that would do this? I honestly expected that the database connection would retry by default since it's kinda annoying to break the app if for a split second the connection drops.
I deployed my java web application in kubernetes using DEPLOYMENTS and was able to scale it and connect it to a database POD, but then I wanted to scale the database too but as you know is not possible in kubernetes and the MYSQL REPLICA not recommended for production. So I tried vitess and was able to scale my database but don't know how or where should I create my java web application DEPLOYMENTS/REPLICAS and connect them to the database through vtgate.
And is there another way of scaling mysql database through kubernetes ?
It's important to note that Vitess is not a transparent proxy that you can just insert between the app and MySQL at the connection level. Vitess turns a set of MySQL servers into a clustered database, and it requires you to build your app against a Vitess driver instead of the plain MySQL driver.
If you're already using JDBC, you shouldn't need a lot of code changes other than connection management, since there is a Vitess implementation of the JDBC interface. However, some query constructs may not be supported yet by Vitess, so you may need to rewrite them into an equivalent form that is supported.
Once your app is compatible with Vitess, deploying it in Kubernetes will be the same as you did before, except you will point the app pods to connect to the VTGate service via DNS.
As for other ways to scale MySQL in Kubernetes without Vitess, there's an important new feature entering Beta in Kubernetes 1.5 called StatefulSet that will help you scale databases like MySQL similar to the way a Deployment can scale stateless Pods. Vitess itself will also become more convenient to scale in Kubernetes by taking advantage of StatefulSet.
However, StatefulSet with pure MySQL will mostly only help you scale read-only traffic by increasing the number of slaves. If you need to scale write traffic, you will likely need to implement application-defined sharding. At that point, the required changes to your app will almost certainly be much more than if you modify it to support Vitess.
I have a java web application that needs to control multiple MySQL databases (same schema) at the same time.
I’m using Tomcat as a web container and hibernate. Connecting up to 3 databases works fine for both reading and writing to the databases.
As soon as I attach the 4th database and write to this database the connection gets lost. I think that this is because of some limitation somewhere but I don’t have the slightest idea what and where.
I would appreciate any help on this.
I am responsible for maintaining a bunch of legacy code which involves several deployed servlets; some through Tomcat 5.5 (yes I know - I did say legacy code!) and others through Glassfish 3.3. All connect using the ojdbc14 driver, to Oracle 10 databases. All use pooled connections.
However, occasionally, the connection to the DB is dropped, the DB restarts, etc., and the servlets fail until someone notices and they are re-deployed. Not. Cool. I understand there is some way to monitor the DB connection the servlet makes, so that, in the event of a lost connection, the servlet 'knows' - how do I accomplish this? And, can I make the servlet smart enough to recover itself by reinstigating the DB conection? I've searched around for an answer, but I have not been successful. Suggestions and pointers muchly appreciated.. I'm a bit of a newbie at this. Thanks!
I'm a Java EE developer and we typically use Weblogic to deploy our apps. Now I'm faced with a new desktop application which requires logging, database connectivity and mail.
After some investigation I'm realizing that desktop apps are a completely new world to me and I'm not sure if I'm choosing the right libraries to support my app.
These are my questions:
In our Weblogic projects we used Log4j and I want to use it again in my desktop app. Is it a bad idea? Should I use a better logging framework?
In Weblogic we retrieve database connections with JNDI but now it seems impossible to do the same. How do I perform the same action in a desktop application so I can connect with a remote database? Is the combination c3p0 + database driver a good approach for this?
Is there any framework/JAR which provides all this stuff (log + ddbb + mail) as an integrated solution? Workmates told me Spring could help. I also found Warework.
In our Weblogic projects we used Log4j and I want to use it again in
my desktop app. Is it a bad idea? Should I use a better logging
framework?
No, it is not a bad idea and perfectly works. Personally, I'd go with java.util.logging as it does the job fairly well and it reduces your applications' footprint (storage). Although, it's configuration is a bit tricky.
In Weblogic we retrieve database connections with JNDI but now it
seems impossible to do the same. How do I perform the same action in a
desktop application so I can connect with a remote database? Is the
combination c3p0 + database driver a good approach for this?
You can directly connect to your database using pure java.sql JDBC API (tons of examples available in the internet), but always have to distribute the proprietary database drivers as part of your application (mySQL, Oracle, DB2, etc.). Furthermore it's possible to directly use connection pools provided with those drivers by using their proprietary APIs (fairly easy to encapsulate). Nevertheless, there are a number of issues:
latency; database protocols are fairly sensitive when it comes to latency (distance between client and database server). Having a database in the UK and desktop clients in US is probably not a good idea.
security 1; you have to distribute database user credentials to each and every desktop client. Be aware of that.
security 2; your database security requirements may demand for transport security (packet encryption).
change management; applying non-backward compatible updates to your database requires you to update all desktop clients (believe me - it's not fun).
network; depending on your environment, certain ports and/or protocols may be blocked.
Is there any framework/JAR which provides all this stuff (log + ddbb +
mail) as an integrated solution? Workmates told me Spring could help.
I also found Warework.
Logging and database access are not an issue and work fairly well without any third-party framework. Of course, those frameworks might provide value regarding other aspects (abstraction, DI, JDBC abstraction, etc.), but this is a topic of detailed software design. Sending emails directly from a desktop application might become an issue, regardless of the framework in use. Just some things to keep in mind:
which SMTP relay server do you want to use?
in case of an enterprise environment, your IT operations teams might not allow you to use their SMTP server from each desktop (keep spam in mind).
Conclusion: In desktop scenarios an application server is not a bad idea either. You should have your desktop application to communicate with an application server only by using e.g. JSON, XML, SOAP over HTTP/HTTPS or RMI, etc. The application should be responsible for the complex tasks like database access, transaction management, fine grained security, email, etc.