I am currently implementing the data-layer for an EE application. I am using latest version of JPA, EclipseLink and Spring. The servlet container is VMware vFabric tc (Tomcat).
As the title mentioned, how should I do the connection pooling part ?
Most tutorials are using Hibernate with an external library like DBCP or C3PO, but EclipseLink tutorials use Springs DriverManagerDataSource that has no connection pooling.
Does EclipseLink have a production level connection pooling feature integrated ?
Do I need to set my datasource with JNDI to use Tomcat's connection pooling ?
Can someone provide guidelines or an example configuration for a similar scenario, please ?
Thank you.
Related
How to bind a JDBC datasource to JNDI context java:comp/env/jdbc only using code-based approach.
We need to write resource-ref in the web.xml for binding a dataSource to JNDI local context java:comp/env/jdbc.
But I want to use only org.springframework.web.context.AbstractContextLoaderInitializer instead of web.xml(the old approach).
We know the method InitialContext#createSubcontext. But some application servers(e.g. Websphere) do not accept to edit the context java:comp/env/jdbc/.
Any solutions?
Versions:
Spring 4.0.7
Servlet 3.0
You could try a full programmatic approach using Spring's SimpleNamingContextBuilder:
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
DataSource ds = new DriverManagerDataSource(...);
builder.bind("java:comp/env/jdbc/myds", ds); // you control the datasource
builder.activate();
It's mainly there for test-purposes. If you choose to use it then you need to provide your own connection pool (e.g. Apache's Jakarta Commons DBCP).
I once used for testing when I deployed to cloudbees. see this blogpost
This works in "plain" Tomcat. I don't have a EE server to test with, but you could try it and see what happens (I guess the JNDI binding name has to be unique to the server though).
What is your application server?
See if this helps.
How to use JNDI DataSource provided by Tomcat in Spring?
How I can create web application project which is loading the jndi datasource set in the server configuration? I want to make the web application independent from the server and the database. Is it possible.
You create the DataSource in your web/application server and expose it through JNDI
You configure the following hibernate property:
<property name="hibernate.connection.datasource">java:comp/env/jdbc/MyDataSource</p>
This way the application is decoupled from the JNDI provider. Even the JNDI url can be configured using a Maven property, so switching to an application server (that uses a different JNDI resource pattern) is just a mater of adding a new Maven profile.
One better approach is to set up the DataSource in your application configuration.
#Autowired
private DataSource dataSource;
...
properties.put("hibernate.connection.datasource", dataSource);
This way you can use any connection pooling framework, like the fastest connection pooling framework, HikariCP.
You can even set up DataSource proxies, like:
datasource-proxy, to log SQL queries with PreparedStatement parameters
FlexyPool, to monitor connection pooling usage and adjust the pool size using adapting startegies
Part of my project is to enhance the thread-safety part of my application. I want to be able to store and retrieve data from a mysql database through JDBC Connector/J and I know that I need to use connection pooling for this but my application is not a servlet ... Should I still install Tomcat and change the config.xml file for the connection pooling datasource, connection numbers etc...?
You don't need a servlet or webapp to use db connection pooling. I'm sure there are a plenty of pools to use, my default is apache dbcp http://commons.apache.org/dbcp/
To use dbcp you need to have the commons-dbcp-1.4.jar (for version 1.4) and the commons-pool (http://commons.apache.org/pool/) in your classpath.
A simple way to use pooling, is to use the org.apache.commons.dbcp.BasicDataSource
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUsername(username);
ds.setPassword(password);
ds.setUrl(jdbcUrl);
ds.setInitialSize(4);
Then, you can get connections out of the pool by calling ds.getConnection() .
Furthermore, you need to configure the maximum count of active connections, have a look at the BasicDataSource API.
You can use c3p0 instead.
You can find the entire documentation here :
c3p0 - JDBC3 Connection
I'm using spring to connect to mysql currently.
I'm thinking of moving to simply servlets and drop spring as I don't need 99% of spring's functionality.
What do you suggest I use to get connection pooling functionality? Is there a mysql connection pool that is framework independent?
Even if you don't need 99% of Spring's features you can still use Spring JDBC which by itself is worthwhile. You don't need the whole Spring infrastructure to use it either - you can drop it in and use it by itself...no DI required. I have a coworker who is using Stripes as his app's framework but uses Spring JDBC for database access.
You don't say what your container is (e.g. Tomcat, JBoss, etc) but there are several container independent connection pools to choose from, such as DBCP, c3p0, BoneCP. If you're using Tomcat 7 it ships with a new connection pool called The Tomcat JDBC Connection Pool (I guess their marketing budget was cut :) ).
We just switched from DBCP to Tomcat's connection pool and it works great. We haven't run any benchmarks on it but haven't run into any issues yet either.
I recommend sticking with Spring JDBC even if you use another connection pool, just for the database connection/statement management, disconnected result set, and "free" prepared statements (Spring JDBC creates prepared statements under the hood for you).
BasicDataSource configured in spring
Weblogic datasource
which implementation is better in terms of
Stability
Performance
scalability
Online Help
I wouldn't even consider using BasicDataSource when using WebLogic Server, its connection pool just rocks:
it's extremely stable, rock solid (one of the top reasons to use it);
it has a great set of features (can't think of any missing one);
it performs very well, no dead lock issues;
it's clusterable;
the administration, configuration are easy and they can be automated with WLST;
monitoring is easy (via JMX);
it's well documented;
it's supported by BEA.
For me, this is a no match.
PS: Of course, this answer applies when running inside WebLogic (which is assumed since the question is about WebLogic connection pool). In your IDE or in a testing context, use whatever you want, e.g. no connection pool at all.
It's much simpler to test and work with Apache's BasicDatasource from your IDE, since you only need the datasource jar file in your classpath. It's not necessary to deploy to an application container.
com.mchange.v2.c3p0.ComboPooledDataSource and the datasource that's bundled with the tcServer is also highly scalable.
The BasicDatasource and the ComboPooledDataSource also gives you the ability to use the same datasource in all environments and it's not coupled to the application container.
And at least the datasource that's bundled with the tcServer is supported by SpringSource.
The ComboPooledDataSource and the Spring alternative are also easy to manage and monitor with JMX.
On the other side, if you're using XA transactions and Weblogic's JTA transaction manager, then you should also use WebLogic's datasource.