How to manage connection to database? - mysql

I should manage a connection to a database from my web application (Tomcatserver/frameworkJSF) ,theconnection from my login page until the deconnection.
I have found that I should let the container manage the connection/deconnection to the database but the example that I found show how to configure tomcat server (http://christophej.developpez.com/tutoriel/j2ee/pooltomcat/)and use the connection from a servlet but I want to use the connection from my beans.
how can I instanciate a connection in my classess each time without creation of a new connection?

You create a datasource in Tomcat or somewhere else which is a pool of connections. You take a connection from that pool and then release it when you're done with it. Tomcat example (in context.xml):
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/yourname"
password="..." type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/mydb?characterEncoding=utf8"
username="..."/>
This creates a pool with a JNDI name 'jdbc/yourname'. You can get this from Spring like this:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/yourname" />
This will create a DataSource object that you can inject into your Java code as needed.
Or there are plenty of JNDI lookup examples on the web to do this programmatically instead of declaratively.
There are also loads of pool implementations, like c3po.

Related

How to configure Tomcat to Not use Database Connection Pooling

How can you configure Tomcat to Not use Database Connection Pooling ?
I want to do this because I have an application that makes very few calls to the mysql database, typically less than 10 in a 24 hour period and some calls fail on retrieving the connection. It appears that Mysql is sometimes giving up on the connection because it has been idle for so long without telling the database pooler.
Worth noting that I have another application that makes thousands of requests to the exact same database and this never fails, so I am convinced the issue is related to the rarity of calls to the database. Mysql is probably part of the problem but updating to Postgres would be a larger task than I want to commit to right now.
So I tried configuring context.xml to testWhileIdle but it is stil failing
<Context path="/store" privileged="true">
<Resource name="jdbc/myapp" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="100" maxWait="10000" testWhileIdle="true"
username="usrnm" password="pwd" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myapp"/>
</Context>
So now I want to disable pooling so its get a new connection every time but I cant work out how to do it, Ive looked at
https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#Plain_Ol%27_Java
but everything seems to be centered out database pooling.
Attempted Solution
So I tried out suggestion in answer and will see what happens over next few days
<Context path="/store" privileged="true">
<Resource name="jdbc/myapp" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="10"
testWhileIdle="true" validationQuery="select 1" validationQueryTimeout="5"
username="usrnm" password="pwd"
driverClassName="com.mysql.jdbc.Driver" testOnBorrow="true"
url="jdbc:mysql://localhost:3306/myapp"/>
</Context>
Nothing should be preventing you from making your own connections and using them. But, I think you are missing configuration to make the connection validation work properly. When a connection is tested and found to be bad it should be replaced by a new one prior to the pool giving you the connection. I think you are missing validationQuery which would be utilized when testing the connection. Check out the docs at https://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html and look at supplying the necessary values for the validation to work. Try testOnBorrow and validationQuery so that each time you go for a connection it will validation it prior to returning. Also, you appear to be setting up way too many connections based on your frequency of usage. Tuning that should help avoid keeping unnecessary connections open.

Using Tomcat JNDI + HAProxy + MySQL Cluster

I have a MySQL Cluster that sits behind a HAProxy
for NON-SSL, I've configured my Tomcat JNDI Global Resource( HikariJNDIFactory) to connect to the HAProxy. everything works...
Now I want to perform SSL-Termination/SSLOffload betwen a secure connection from Tomcat JNDI to HAProxy.
I've searched all over, all i see is pass through SSL, that requires MySQL Cluster to use SSL(which i don't want to do.).
is this possible.?
server.xml
...
<Resource name="jdbc/secondDataSource"
auth="Container"
factory="com.zaxxer.hikari.HikariJNDIFactory"
type="javax.sql.DataSource"
dataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
driverClass="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://url2:3306/schema?autoReconnect=true"
dataSource.user="user2"
dataSource.password="pwd2"
minimumIdle="5"
maximumPoolSize="50"
connectionTimeout="300000"
maxLifetime="1800000"
connectionTestQuery="SELECT 1 FROM DUAL" />
...
i have tomcat starting with SSL, however, I will not connect to HaProxy over SSL.
what would be the configuration for HaProxy to make this work.(my SQL Nodes behind the proxy are using private Ips(10.0.x.x) )

Tomcat, Hibernate, ElasticBeanStalk, RDS, MySql Database connection

I'm moving my application to AWS ElasticBeanStalk and after spending countless hours trying to get my database connection working, I've repetitively failed.
First attempt,
I currently have an existing RDS database in use which I would like to continue use. I tried to connect to it with a plain old jdbc connection as followed.
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://masdfwe.czwweehqejmbr.us-east-1.rds.amazonaws.com:3306/project</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">xxxxx</property>
But found the following exception in the log.
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://masdfwe.czwweehqejmbr.us-east-1.rds.amazonaws.com:3306/project
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:187)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
... 125 more
The mysql driver is deff in the lib directory, so I'm not sure why I'm seeing this exception.
Do I need to create a JNDI connection? If so, how do I go about altering the configuration files in tomcat7 on ElasticBeanStalk? Am I suppose to ssh into the ec2 instance, or do I do it into the ElasticBeanStalk instance?
Would I use something like this in my hibernate.cfg.xml file?
<property name="hibernate.connection.datasource">java:comp/env/jdbc/project</property>
web.xml
<resource-ref>
<description>MyDatabase Description</description>
<res-ref-name>jdbc/project</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I'm just not sure where or what I'm suppose to be configuring. Any help would be appreciated, I'm very confused.
Thanks in advance.
You shouldn't have to much about with JNDI to get your RDS connection to work. From your description, I'm also a bit puzzled as to what's not working for you. I've set up my JDBC/RDS connections with no problems, although I've used Spring to access the database, so details are handled by Spring's data sources.
Just an idea: You do have a
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
to load your JDBC driver in your code?
Good luck!
Your assumption is right. With Hibernate you should be able to use your configuration as is. No need to instantiate driver by name, it applies to Java code without Hibernate.
Your configuration settings look right.
Could it be that the jar with MySQL connector somehow got damaged? Is there anything else in stacktrace?
I had a similar sounding problem with RDS and MySQL. I tried copying the driver's jar file and manipulating the classpath but none of that seemed necessary nor did it help. Eventually, I changed the code where my DataSource gets initialized to pass in my connection string rather than each property individually (e.g., ds.setUser( xxx )). This resolved my issue.
So in the end, my properties file contains these properties:
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc\:mysql\://xyz.rds.amazonaws.com:3306/schema?user=username&password=mypassword
And my data source config:
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass( jdbcDriverClassName );
ds.setJdbcUrl( jdbcUrl );
// other ds configuration parameters follow
With this change, I was able to overcome the "driver" exception and connect to the RDS database as expected.

Java web application running on Tomcat unable to execute SQL queries with DB Connection pooling

I've come across similar questions many times here on Stack Overflow, however, I haven't been able to get it right.
I'm trying to use Database connection pooling in Tomcat 6 + MySQL ( on AWS RDS )
These are the parameters I have configured.
( I'm closing the connection from my Java code also )
<Resource name="jdbc/awsDB" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
initialSize="10"
testWhileIdle="true"
maxActive="30"
maxAge="3600"
maxIdle="5"
maxWait="3000"
removeAbandoned="true"
logAbandoned="false"
validationQuery="SELECT 1"
removeAbandonedTimeout="60"
timeBetweenEvictionRunsMillis="15000"
username="sbose78" password="XXXXX"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://xxxxrds.amazonaws.com:3306/health?autoReconnect=true" />
The application works great for the first few database queries and after that the database connection hangs indefinitely.
From the connection logs I've noticed that the connection pooling works good , however, Even when there are 10+ connections in SLEEP state, a new database query takes indefinite time to execute.
I end up re-starting the server in most cases.
What configuration mistake am I doing?
Thanks a lot in advance!
Check whether your query is using all the indexed columns in the where clause.If you are sure about the connection pool, then most probaably the problem will be with query itself.

Grails / Tomcat: Database connections get stuck in sleep state

I have a Grails application running under Tomcat 7 and am having problems managing MySQL connections.
The problem is that each new request to the application (i.e., a page load) is creating a new MySQL connection, and these connections are not closing. Instead, they stay in a SLEEP state until the MySQL server finally refuses to accept more connections. Therefore, simply by reloading individual pages on the site at a fast rate, one can create numerous database connections.
So it seems that my connection pool isn't closing the connections with MySQL fast enough. There are a number of configuration settings for the connection pool, but I'm not sure what needs to be tuned to avoid this problem.
Here is the configuration from my context.xml file:
<Resource name="jdbc/Production" auth="Container" type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
minEvictableIdleTimeMillis="1800000"
timeBetweenEvictionRunsMillis="1800000"
numTestsPerEvictionRun="3"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
testOnBorrow="true"
testWhileIdle="true"
testOnReturn="true"
validationQuery="SELECT 1"
username=""
password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/Production"
/>
Thanks very much for any suggestions.
You did not define a connection pool.
Add following code to the context.xml(it seems to be a JNDI data source):
pooled = "true"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
If you haven't put JDBC Pool in to your dependency configuration, add following to the plugins closure in BuildConfig.groovy:
compile ":jdbc-pool:1.0.9.3"
You may use other connection pools, but JDBC Pool will be my recommendation.