Spring 4 Hikari Connection Pool ClassCastException - spring

I wish to use Hikari Connection Pool in my Spring 4 application. The database is Google Cloud SQL Postgres database.
I have the following dependency in pom.xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.1</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>
In my applicationContext.xml, I have:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="org.postgresql.Driver" />
<property name="maximumPoolSize" value="10" />
<property name="idleTimeout" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="url">jdbc:postgresql://google/mydatabase?cloudSqlInstance=projectId:regionName:myInstance&socketFactory=com.google.cloud.sql.postgres.SocketFactory</prop>
<prop key="user">postgres</prop>
<prop key="password">mypassword</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
But I'm getting the following exception:
Caused by: java.lang.ClassCastException: Cannot cast org.postgresql.Driver to javax.sql.DataSource
at java.lang.Class.cast(Class.java:3369)
at com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:102)
What could be going wrong?

The org.postgresql.jdbc.Driver is not a javax.sql.DataSource, it is a java.sql.Driver, so it doesn't work for the property dataSourceClassName as that property expects a javax.sql.DataSource class name.
If you want to use the driver (and not a DataSource), then you should use the property driverClassName.
So:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="driverClassName" value="org.postgresql.Driver" />
...

Use Hikari recommended datasource for PostgreSQL: org.postgresql.ds.PGSimpleDataSource
<property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource" />
Database Driver DataSource class
PostgreSQL pgjdbc-ng com.impossibl.postgres.jdbc.PGDataSource
PostgreSQL PostgreSQL org.postgresql.ds.PGSimpleDataSource

Related

Atomikos, Hibernate 4/5, Spring 4, Jetty - unable to locate current JTA transaction

I have tried many different solutions, but getting the exception:
org.hibernate.HibernateException: Unable to locate current JTA transaction
I am using Atomikos, Hibernate 4/5, Spring 4 and Jetty.
How to configure Spring 4 + Hibernate 4/5 (non JPA)?
Atomikos documentation only has an example with JPA.
I would be very grateful for a working example (non JPA, non JEE Application server).
This example changes non JPA:
<context:annotation-config />
<context:component-scan base-package="com.atomikos.icatch.jta.hibernate4" />
<tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="false" />
<aop:aspectj-autoproxy />
<!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" depends-on="dataSource">
<property name="forceShutdown" value="true" />
</bean>
<!-- Also use Atomikos UserTransactionImp, needed to configure Spring -->
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<!-- by default : looks for java:comp/UserTransaction -->
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="atomikosUserTransaction"></property>
<property name="transactionManager" ref="atomikosTransactionManager"></property>
</bean>
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
destroy-method="close" init-method="init" >
<property name="uniqueResourceName" value="atomikos-standalone" />
<property name="maxPoolSize" value="10" />
<property name="minPoolSize" value="5" />
<property name="testQuery" value="SELECT 1" />
<property name="xaDataSource" ref="xaReferent" />
</bean>
<bean id="xaReferent" class="org.h2.jdbcx.JdbcDataSource">
<property name="URL" value="jdbc:h2:~/test-db;MODE=PostgreSQL;MVCC=TRUE;DB_CLOSE_DELAY=-1" />
<property name="user" value="sa" />
<property name="password" value="" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.atomikos.icatch.jta.hibernate4"/>
<property name="jtaTransactionManager" ref="transactionManager" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.transaction.jta.platform">com.atomikos.icatch.jta.hibernate4.AtomikosPlatform</prop>
<prop key="show_sql" >true</prop>
</props>
</property>
</bean>
</beans>
Update
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.atomikos.icatch.jta.hibernate4"/>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
<prop key="hibernate.transaction.jta.platform">com.atomikos.icatch.jta.hibernate4.AtomikosPlatform</prop>
<prop key="hbm2ddl.auto">create</prop>
<prop key="connection.release_mode">auto</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>

How to set up datasource with Spring for HikariCP?

Hi I'm trying to use HikariCP with Spring for connection pool. I'm using jdbcTempLate and JdbcdaoSupport.
This is my spring configuration file for datasource:
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<property name="dataSourceClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="dataSource.url" value="jdbc:oracle:thin:#localhost:1521:XE"/>
<property name="dataSource.user" value="username"/>
<property name="dataSource.password" value="password"/>
</bean>
But unfortunately the following error message is generating:
Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.zaxxer.hikari.HikariDataSource]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zaxxer.hikari.HikariDataSource.<init>()
Can anyone please tell me how to solve this issue?
you need to write this structure on your bean configuration (this is your datasource):
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="${hibernate.dataSourceClassName}" />
<property name="maximumPoolSize" value="${hibernate.hikari.maximumPoolSize}" />
<property name="idleTimeout" value="${hibernate.hikari.idleTimeout}" />
<property name="dataSourceProperties">
<props>
<prop key="url">${dataSource.url}</prop>
<prop key="user">${dataSource.username}</prop>
<prop key="password">${dataSource.password}</prop>
</props>
</property>
</bean>
<!-- HikariCP configuration -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
This is my example and it is working. You just need to put your properties on hibernate.properties and set it before:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:hibernate.properties</value>
</list>
</property>
</bean>
Obs.: the versions are
log4j: 1.2.16
springframework: 3.1.4.RELEASE
HikariCP: 1.4.0
Properties file (hibernate.properties):
hibernate.dataSourceClassName=oracle.jdbc.pool.OracleDataSource
hibernate.hikari.maximumPoolSize=10
hibernate.hikari.idleTimeout=30000
dataSource.url=jdbc:oracle:thin:#localhost:1521:xe
dataSource.username=admin
dataSource.password=
my test java config (for MySql)
#Bean(destroyMethod = "close")
public DataSource dataSource(){
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/spring-test");
hikariConfig.setUsername("root");
hikariConfig.setPassword("admin");
hikariConfig.setMaximumPoolSize(5);
hikariConfig.setConnectionTestQuery("SELECT 1");
hikariConfig.setPoolName("springHikariCP");
hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
return dataSource;
}
You can create a datasource bean in servlet context as:
<beans:bean id="dataSource"
class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<beans:property name="dataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<beans:property name="maximumPoolSize" value="5" />
<beans:property name="maxLifetime" value="30000" />
<beans:property name="idleTimeout" value="30000" />
<beans:property name="dataSourceProperties">
<beans:props>
<beans:prop key="url">jdbc:mysql://localhost:3306/exampledb</beans:prop>
<beans:prop key="user">root</beans:prop>
<beans:prop key="password"></beans:prop>
<beans:prop key="prepStmtCacheSize">250</beans:prop>
<beans:prop key="prepStmtCacheSqlLimit">2048</beans:prop>
<beans:prop key="cachePrepStmts">true</beans:prop>
<beans:prop key="useServerPrepStmts">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
Using XML configuration, your data source should look something like this:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="dataSourceProperties" >
<props>
<prop key="dataSource.url">jdbc:oracle:thin:#localhost:1521:XE</prop>
<prop key="dataSource.user">username</prop>
<prop key="dataSource.password">password</prop>
</props>
</property>
<property name="dataSourceClassName"
value="oracle.jdbc.driver.OracleDriver" />
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<constructor-arg ref="hikariConfig" />
</bean>
Or you could skip the HikariConfig bean altogether and use an approach like the one mentioned here
I have recently migrated from C3P0 to HikariCP in a Spring and Hibernate based project and it was not as easy as I had imagined and here I am sharing my findings.
For Spring Boot see my answer here
I have the following setup
Spring 4.3.8+
Hiberante 4.3.8+
Gradle 2.x
PostgreSQL 9.5
Some of the below configs are similar to some of the answers above but, there are differences.
Gradle stuff
In order to pull in the right jars, I needed to pull in the following jars
//latest driver because *brettw* see https://github.com/pgjdbc/pgjdbc/pull/849
compile 'org.postgresql:postgresql:42.2.0'
compile('com.zaxxer:HikariCP:2.7.6') {
//they are pulled in separately elsewhere
exclude group: 'org.hibernate', module: 'hibernate-core'
}
// Recommended to use HikariCPConnectionProvider by Hibernate in 4.3.6+
compile('org.hibernate:hibernate-hikaricp:4.3.8.Final') {
//they are pulled in separately elsewhere, to avoid version conflicts
exclude group: 'org.hibernate', module: 'hibernate-core'
exclude group: 'com.zaxxer', module: 'HikariCP'
}
// Needed for HikariCP logging if you use log4j
compile('org.slf4j:slf4j-simple:1.7.25')
compile('org.slf4j:slf4j-log4j12:1.7.25') {
//log4j pulled in separately, exclude to avoid version conflict
exclude group: 'log4j', module: 'log4j'
}
Spring/Hibernate based configs
In order to get Spring & Hibernate to make use of Hikari Connection pool, you need to define the HikariDataSource and feed it into sessionFactory bean as shown below.
<!-- HikariCP Database bean -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<!-- HikariConfig config that is fed to above dataSource -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="SpringHikariPool" />
<property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource" />
<property name="maximumPoolSize" value="20" />
<property name="idleTimeout" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="serverName">localhost</prop>
<prop key="portNumber">5432</prop>
<prop key="databaseName">dbname</prop>
<prop key="user">dbuser</prop>
<prop key="password">dbpassword</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
<!-- Your Hikari dataSource below -->
<property name="dataSource" ref="dataSource"/>
<!-- your other configs go here -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.provider_class">org.hibernate.hikaricp.internal.HikariCPConnectionProvider</prop>
<!-- Remaining props goes here -->
</props>
</property>
</bean>
Once the above are setup then, you need to add an entry to your log4j or logback and set the level to DEBUG to see Hikari Connection Pool start up.
Log4j1.2
<!-- Keep additivity=false to avoid duplicate lines -->
<logger additivity="false" name="com.zaxxer.hikari">
<level value="debug"/>
<!-- Your appenders goes here -->
</logger>
Logback
Via application.properties in Spring Boot
debug=true
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
Using logback.xml
<logger name="com.zaxxer.hikari" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
With the above you should be all good to go! Obviously you need to customize the HikariCP pool configs in order to get the performance that it promises.
This last error is caused by the library SLF4J not being found. HikariCP has two dependencies: slf4j and javassist. BTW, HikariDataSource does have a default constructor and does not need HikariConfig, see this link. So that was never the problem.
I found it in http://www.baeldung.com/hikaricp and it works.
Your pom.xml
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.3</version>
</dependency>
Your data.xml
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.databaseurl}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"
/>
Your jdbc.properties
jdbc.driverClassName=org.postgresql.Driver
jdbc.dialect=org.hibernate.dialect.PostgreSQL94Dialect
jdbc.databaseurl=jdbc:postgresql://localhost:5432/dev_db
jdbc.username=dev
jdbc.password=dev
May this also can help using configuration file like java class way.
#Configuration
#PropertySource("classpath:application.properties")
public class DataSourceConfig {
#Autowired
JdbcConfigProperties jdbc;
#Bean(name = "hikariDataSource")
public DataSource hikariDataSource() {
HikariConfig config = new HikariConfig();
HikariDataSource dataSource;
config.setJdbcUrl(jdbc.getUrl());
config.setUsername(jdbc.getUser());
config.setPassword(jdbc.getPassword());
// optional: Property setting depends on database vendor
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
dataSource = new HikariDataSource(config);
return dataSource;
}
}
How to use it:
#Component
public class Car implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(AptSommering.class);
#Autowired
#Qualifier("hikariDataSource")
private DataSource hikariDataSource;
}
for DB2, please try below configuration.
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="dataSourceClassName" value="com.ibm.db2.jcc.DB2SimpleDataSource"/>
<property name="maximumPoolSize" value="${db.maxTotal}" />
<property name="dataSourceProperties">
<props>
<prop key="driverType">4</prop>
<prop key="serverName">192.168.xxx.xxx</prop>
<prop key="databaseName">dbname</prop>
<prop key="portNumber">50000</prop>
<prop key="user">db2inst1</prop>
<prop key="password">password</prop>
</props>
</property>
<property name="jdbcUrl" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>

Spring / Hibernate 4(non JPA) / Infinispan / Atomikos no transaction manager found

I'm using Tomcat7 and need JTA for Infinispan Cache. I am using Spring 3.x with Hibernate 4 (non JPA) and Atomikos for JTA. I can't find a transaction manager look up class in the Atomikos library or docs for Hibernate 4. All examples are for Hibernate version 3 or with use of JPA. Infinispan cannot find a transaction manager.
Here is my config:
<bean id="myDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="rsname" />
<property name="xaDataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="URL">${db.url}</prop>
<prop key="user">${db.user}</prop>
<prop key="password">${db.pass}</prop>
</props>
</property>
<property name="maxPoolSize" value="50" />
<property name="minPoolSize" value="20" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingLocations" value="classpath*:hibernate/**/*.hbm.xml" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.cache.region.factory_class=org.hibernate.cache.infinispan.InfinispanRegionFactory
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
</value>
</property>
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction">
<property name="transactionTimeout" value="300" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" depends-on="atomikosTransactionManager,atomikosUserTransaction">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="allowCustomIsolationLevels" value="true" />
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" rollback-for="Throwable" />
</tx:attributes>
</tx:advice>
Looking at the spring docs setting the jtaTransactionManager property on the LocalSessionFactoryBean should take care of things.
<property name="jtaTransactionManager" ref="transactionManager" />
Basically adding that to the definition of your bean should be enough.
Another note about your configuration, you are using <tx:annotation-driven /> so basically your <tx:advice .. /> does nothing only takes up space.

java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor

i am trying to migrate to hibernate 4.1.0.Final with spring 3.1.1.RELEASE
and following is my configuration for hibernate:
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="${project.groupId}.domain" />
<!-- control the behavior of Hibernate at runtime,All are optional and
have reasonable default values -->
<property name="hibernateProperties">
<value>
<!-- hibernate.dialect: allows Hibernate to generate SQL optimized for
a particular relational database -->
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=create-drop
hibernate.show_sql=false
hibernate.jdbc.fetch_size=100
hibernate.jdbc.batch_size=100
hibernate.jdbc.batch_versioned_data=true
hibernate.order_inserts=true
hibernate.order_updates=true
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=false
</value>
</property>
</bean>
<!-- provides properties to hibernate to make it able to create session
factory. Hibernate uses instance of session bean of type -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<!-- responsible for creating sessionFactory opening transactions and binding
them to the current thread context. -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="nestedTransactionAllowed" value="true" />
</bean>
<!-- get exception translation from HibernateException into DataAccessException
hierarchy -->
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
but when trying to run the application, i got the following exception:
java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
please advise why i get this error, and how to fix it, thanks.
Try using the org.springframework.orm.hibernate4.HibernateTransactionManager
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>

java:comp/UserTransaction is not bound in the context using btm and spring

I am trying to configure bitronix as my JTA manager with spring like this:
<!-- Bitronix Transaction Manager embedded configuration -->
<bean id="xaDataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource"
init-method="init" destroy-method="close">
<property name="className" value="bitronix.tm.resource.jdbc.lrc.LrcXADataSource" />
<property name="uniqueName" value="java/DS1" />
<property name="maxPoolSize" value="5" />
<property name="minPoolSize" value="0" />
<property name="allowLocalTransactions" value="false" />
<property name="testQuery" value="SELECT 1 FROM DUAL" />
<property name="driverProperties">
<props>
<prop key="user">user</prop>
<prop key="password">user</prop>
<prop key="url">url</prop>
<prop key="driverClassName">oracle.jdbc.OracleDriver</prop>
</props>
</property>
</bean>
<bean id="btmConfig" factory-method="getConfiguration"
class="bitronix.tm.TransactionManagerServices">
<property name="serverId" value="spring-btm" />
</bean>
<bean id="bitronixTransactionManager" factory-method="getTransactionManager"
class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig,xaDataSource"
destroy-method="shutdown" />
<bean id="jtaTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="bitronixTransactionManager" />
<property name="userTransaction" ref="bitronixTransactionManager" />
</bean>
<!-- end of Bitronix Transaction Manager embedded configuration -->
I see that bitronix is starting and running:
399 DEBUG ( PoolingDataSource.java:84) - building XA pool for java/DS1 with 0 connection(s) etc..)
However when I try:
DataSource ds = (DataSource) ic.lookup("java:comp/UserTransaction");
I get naming exception for it.
WHy is it? what else do I need to configure?
EDITED
I saw in tomcat documentation that the <Transaction> can bound the transaction manager to take care of it.
wouldn't it be a conflict that I define it both in tomcat and in spring?
Try adding this in your web app's META-INF/context.xml:
<Context>
<Transaction factory="bitronix.tm.BitronixUserTransactionObjectFactory" />
</Context>
This should bind the transaction manager under the java:comp/UserTransaction name for this particular web app only.

Resources