How to apply connection pooling? - java

I am specific about this tutorial: The BalusC Code: DAO tutorial - use in JSP/Servlet
How can I apply connection pooling together with this code? What will be the changes in the code?

I'm not sure about Tomcat, but at least Application Servers such as GlassFish come with connection pool functionality integrated. You just need to configure the connection details in the admin interface. Then in your EJB use:
#PersistenceContext
private EntityManager em;
The container will take care of the connection pooling.

Related

Intercept connection pooling of Datasource connections in JEE container

is it possible to intercept the connection pooling mechanism of a DataSource in a JEE container?
For (un)setting some information on the connection's context I'm searching for a way to intercept the pooling mechanism so that I know when and which connection is put back into the pool.
So does anyone know a (common) way to do this?
Some additional info:
The application runs on Wildfly
Using Hibernate for ORM
The option connection-listener in datasource configuration can be the solution.
connection-listener:
An org.jboss.jca.adapters.jdbc.spi.listener.ConnectionListener that
provides a possible to listen for connection activation and
passivation in order to perform actions before the connection is
returned to the application or returned to the pool
You can create a custom implementation of org.jboss.jca.adapters.jdbc.spi.listener.ConnectionListener and deploy it as a module to do that you want.

What are the relationships between JTA provider like Atomikos and connection pool like HikariCP?

I'm reading Java Persistence with Hibernate, and I found the following text.
Today, high-quality standalone JTA providers such as Bitronix (used for the example code of this book) and Atomikos are available and easy to install in any Java environment. Think of these solutions as JTA-enabled database connection pools.
As I understand, JTA providers have their own connection pools.
So, do they integrate (how, if they do) with connection pools like HikariCP and C3P0? Thank you.
The answer is NO, you cannot combine JTA provider with these JDBC Connection Pools.
The short reason is:
The JTA provider need XADataSource and the JDBC Connection Pools named by you just have standard DataSource.
The longer reason is:
With a JTA provider you want to handle global transactions - global means over different DataSources. (e.g. your operation wants to do something in database/DataSource 1 and something in database/DataSource 2 - if one of these parts fail, you want both parts to get rolled back as if nothing has happened to both databases/DataSources) This is done by Two-Phase-Commit and this needs a XADataSource.
Your JDBC connection pools are lightweight for applications using only one DataSource - for this applications you do not need JTA (even if you can use them either, of course).

Adavantage of using connection from Data Source over JDBC Driver Manager

Question is basically identify the best practices on data access layer.
I want to choose in between using a data source or traditional driver manager to load the the connection on web applications. I know very clearly following advantages
Flexibility of configuration
In built connection pooling mechanism
But if I can sacrifice advantage of flexibility with configuration and have own connection pooling mechanism, Do I get any other benefit out of data source. In other way around what are limitations or issues I would face while having application managed jdbc driver connection than container managed.
I know the question is so stupid that I should be knowing the advantage of somebody takes care of handling connection than my application. But this is rare scenario where I can't use datasource in web application. I would be looking following things
How better I can design own connection pool my self?
Is there any thing else I should take care when I access connection through DriverManager API
Note that is is very possible to programmatically create a DataSource (backed by a connection pool) dynamically based on user input.
Using Apache Commons-dbcp:
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(DATABASE_DRIVER_CLASS);
ds.setUsername(DATABASE_USERNAME);
ds.setPassword(DATABASE_PASSWORD);
ds.setUrl(DATABASE_URL);
ds.setInitialSize(1);
ds.setMaxActive(50);
ds.setDefaultAutoCommit(false);
So I think the question is not really between a DataSource and no-DataSource, but rather between a container managed DataSource and an application managed DataSource.
Container managed DataSources are easier to manage by server-admin types. They can be tuned through the app server web UI, etcApplication managed DataSources do not have this advantage.

DAO and Connections

Where I should open and close connections to repositories? In article "Dont repeat the DAO" written:
The DAO is not responsible for handling transactions, sessions, or
connections. These are handled outside the DAO to achieve flexibility.
But some people suggest me to inject #Resource DataSource object into DAO classes and handle all connections inside DAO methods...i.e. every CRUD operation should open and close connection to repository.
Take the recommendation from the article.
Preferably you want your application container to manage Resources and connection pooling. If your connection has transaction management configured, then the container will be able to manage your transaction (hence no need to open connection and do commits).
If you're managing connection and transaction yourself, open the connection first, enable transaction and pass the opened connection to the DAO, commit transaction, then close connection (outside of the DAO).
Once your service finished with all DAO's, close the connection.
Hope this helps.
I personally use Spring to manage the data sources.
Configure the data source bean in your application context xml. Autowire it to the DAO and then use Spring to handle the transactions with the #Transactional annotation on the DAO class.
You also need <tx:annotation-driven/> in your application context.
If you are using Hibernate you can do the same by configuring the SessionFactory in your application context.
I agree that you can use spring for managing transactions but at the same time keep in mind that managing transactions is not a duty of DAOs. They should be handled in your service layer which is responsible for managing business logic.

Is it possible to use bitronix PoolingDataSource without BTM?

Is it possible to use bitronix.tm.resource.jdbc.PoolingDataSource without using bitronix transaction manager and using standalone JBossTS instead?
For database access I use Hibernate, with transaction demarcation done with Spring's #Transactional annotation (or Spring's TransactionTemplate which has similar implementation). PoolingDataSource and standalone JBossTS is used in tests, however I'd like not to abandon db connection pooling.
If it's not possible, what other pooling data source will fit here? Some other question suggests c3p0 is not an option. Is it true?
No, that's not possible and it's also not possible to switch XA pools between transaction managers simply because there is no standard defining the communication between the transaction manager and the JDBC connection pool. At least that's the short story, the long one is here: http://blog.bitronix.be/2011/02/why-we-need-jta-2-0/
AFAIK in the JBossTS case your only options are to use the JBossAS connection pool but that would not be a minor achievement as it requires at least a JCA runtime, but certainly more.
I'm afraid the only realistic options are to use all of BTM or JBossTS without connection pooling or JBossTS with pooling but inside JBossAS.

Categories

Resources