Is it possible to use bitronix PoolingDataSource without BTM? - java

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.

Related

Can we use Atomikos Transaction Manager with Tomcat JDBC XA Connection Pool

I need to use Atomikos transaction Manager with Tomcat 8.0.36 to support JTA. Every documentation for Atomikos, recommend using com.atomikos.jdbc.AtomikosDataSourceBean as the type and com.atomikos.tomcat.EnhancedTomcatAtomikosBeanFactory as the object factory for the datasource resource (specified as resource in tomcat's context.xml)
However, if we use com.atomikos.jdbc.AtomikosDataSourceBean, Atomikos will use its own JDBC connection Pool instead of Tomcat's connection Pool.
Tomcat's connection pool provides more configurable settings than atomikos.
Is it possible to use Datasource resource, with type as javax.sql.XADatasource and factory as org.apace.tomcat.jdbc.pool.DatasourceFactory (which will use Tomcat's XA connection Pool) with Atomikos?
I tried to use atomikos with tomcat JDBC pool i.e. I didn't use AtomikosDatasourceBean (which is the only recommended way in Atomikos documentation). So far I have tested this with 3-4 applications and it seems to work fine.
Atomikos documentation doesn't provide much detail about it, however, there is one sentence on its website which says that we can use other JDBC pool with tomcat.

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).

Using spring to connect to mysql, how to mimick connection pool?

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).

DataSource or ConnectionPoolDataSource for Application Server JDBC resources

When creating JNDI JDBC connection pools in an application server, I always specified the type as javax.sql.ConnectionPoolDataSource. I never really gave it too much thought as it always seemed natural to prefer pooled connections over non-pooled.
However, in looking at some examples (specifically for Tomcat) I noticed that they specify javax.sql.DataSource. Further, it seems there are settings for maxIdle and maxWait giving the impression that these connections are pooled as well. Glassfish also allows these parameters regardless of the type of data source selected.
Are javax.sql.DataSource pooled in an application server (or servlet container)?
What (if any) advantages are there for choosing javax.sql.ConnectionPoolDataSource over javax.sql.DataSource (or vice versa)?
Yes, Tomcat does use Apache DBCP pooling by default for DataSources defined as JNDI Context resources.
From documentation at
http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#JDBC_Data_Sources
NOTE - The default data source support
in Tomcat is based on the DBCP
connection pool from the Commons
project. However, it is possible to
use any other connection pool that
implements javax.sql.DataSource, by
writing your own custom resource
factory, as described below.
Digging Tomcat 6 sources revealed that they obtain connection factory this way (in case when you don't specify your own using Context's "factory" attribute):
ObjectFactory factory = (ObjectFactory)Class.forName(System.getProperty("javax.sql.DataSource.Factory", "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory")).newInstance();
And org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory that implements javax.naming.spi.ObjectFactory takes care of creating DataSource instances:
http://www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat-dbcp/7.0.2/tomcat-dbcp-7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/BasicDataSourceFactory.java?format=ok
I see they create instances of org.apache.tomcat.dbcp.dbcp.BasicDataSource:
http://www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat-dbcp/7.0.2/tomcat-dbcp-7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/BasicDataSource.java?format=ok
Oddly enough, this class doesn't implement ConnectionPoolDataSource itself, neither does org.apache.tomcat.dbcp.dbcp.PoolingDataSource, that's returned internally by BasicDataSource
http://www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat-dbcp/7.0.2/tomcat-dbcp-7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/PoolingDataSource.java?format=ok
So I presume when you configured your DataSources as javax.sql.ConnectionPoolDataSource you also used some custom-defined factory (it's just a guess, but I suppose otherwise you'd have class cast exceptions in Tomcat, since their pooling doesn't really provide instances of javax.sql.ConnectionPoolDataSource, only javax.sql.DataSource).
Thus, to answer questions about advantages or disadvantages of particular case you should compare Apache DBCP against pooling mechanism in your DataSource factory, whichever one you used.
My understanding is that only purpose of ConnectionPoolDataSource is to give access to PooledConnection which implements native pooling by JDBC driver. In this case application server can implement connections pooling using this native interface.
When using simple DataSource, appserver uses its own pooling instead of native.
Can't say which approach is best.
As for the Java docs it contains this:
DataSource Java 7 API
The DataSource interface is implemented by a driver vendor. There are three types of implementations:
Basic implementation -- produces a standard Connection object
Connection pooling implementation -- produces a Connection object that will automatically participate in connection pooling. This implementation works with a middle-tier connection pooling manager.
Distributed transaction implementation -- produces a Connection object that may be used for distributed transactions and almost always participates in connection pooling. This implementation works with a middle-tier transaction manager and almost always with a connection pooling manager.
PooledConnection Java 7 API
An application programmer does not use the PooledConnection interface directly; rather, it is used by a middle tier infrastructure that manages the pooling of connections.
When an application calls the method DataSource.getConnection, it gets back a Connection object. If connection pooling is being done, that Connection object is actually a handle to a PooledConnection object, which is a physical connection.
The connection pool manager, typically the application server, maintains a pool of PooledConnection objects ....
So in the end you just use DataSource and Connection classes and never PooledConnection / ConnectionPoolDataSource, if you are a happy and normal programmer.
If are implementing an Application Server that's another story...

Implement custom JTA XAResource for using with hibernate

I have two level access to database: the first with Hibernate, the second with JDBC. The JDBC level work with nontransactional tables (I use MyISAM for speed). I want make both levels works within transaction. I read about JTA which can manage distributed transactions. But there is lack information in the internet about how to implement and use custom resource.
Does any one have experience with using custom XAResources?
I want make both levels works within transaction.
Then you'll have to change your storage engine for InnoDB, MyISAM tables do not support transactions (technically, you won't get an error but a rollback won't rollback anything).
Does any one have experience with using custom XAResources?
I'm not sure to understand what you're talking about. The only XA resource I see here is your database and you don't need to implement anything custom. What you need to do is to use XA connections very likely obtained from two XA DataSources (which are supported by MySQL Connector/J 5.0.0+), use the JTA API and let the Transaction Manager do its job .
But to be honest, you should really clarify your requirements. There might be other (and easier) options than using XA. And if all the above sounds like Chinese, then I have the feeling that don't use XA would be a good advice here.
Connection are obtained via a DataSource that can be configured to support distributed transaction or not. To use multiple connections in a distributed transaction, you must configure the multiple DataSource to support XA and return XA connections.
That said, you need several physical connections only if you connects to different database, which doesn't seem to be your case (that's not clear in the question).
A DataSource can be smart enough to make sure that the same physical connection is used as long as you are in the same thread; each time you ask for a connection, it actually returns a "handle" to the same physical connection, and the physical connection returns to the pool when all handles have been closed. (But that depends on the DataSource implementation).
Hibernate per se is not an XA resource: it uses underlying a connection obtained via a DataSource. But it hooks itself in the transaction manager via JTA, in particular to flush all pending changes before the distributed transaction commits.
You can most of the time obtain the underlying connection used by the EntityManager using implementation specific API (it's at least possible with Hibernate). This means that you can maybe fulfill your requirement without using JTA and XA at all: use the underlying connection of the EntityManager for your JDBC stuffs.
In summary:
No need to mess with XAResource
Switch to InnoDB
You can try to switch to a XA DataSource and obtain a connection with DataSource.getConnection()
You can try to switch to a XA DataSource and obtain the underlying EntityManager connection
You can try to stick with a non-XA DataSource and obtain the underlying EntityManager connection
Hope I understood your question right and that it helps.

Categories

Resources