Defining a datasource resource in tomcat context.xml - spring

I'm using Spring batch and spring batch admin and i write in applicationContext this code, defining a bean called dataSource:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" ... />
Now, i would change this code, defining my "dataSource" in the context.xml file of Tomcat and then load it.
I read that this is possible defining a Resource like this:
<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver"
maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/myDatabase"
password="myPass" type="javax.sql.DataSource"
url="jdbc:oracle:thin:#host.com:1521/abc" username="myUser" />
and then loading it in my applicationContext.xml using this:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myDatabase" />
But with this i have a problem. It is possible to define a resource for my dataSource so that it is of class org.apache.commons.dbcp.BasicDataSource and not javax.sql.DataSource?
Any help?
Thank you

Related

Apache Tomcat (TomEE)/8.5.6 (7.0.2) Oracle JTS/XA-Datasource Configuration

Has anybody a running example configuration for:
Apache Tomcat (TomEE)/8.5.6 (7.0.2) PLUME
Oracle 11g (ojdbc7.jar)
to match EE/JPA 2.1-Development as near as possible?
Currently i get TomEE's user database, if i try to use my datasource. I think it's caused by the log entry "org.apache.openejb.config.AutoConfig.deploy Found matching datasource: jdbc/jKgvDS but this one is not a JTA datasource", falling back to the last known running datasource. So how can i configure a valid JTA datasource?
server.xml:
<Resource id="jKgvDS" name="jdbc/jKgvDS"
jtaManaged="true"
auth="Container"
type="oracle.jdbc.xa.client.OracleXADataSource"
driverClassName="oracle.jdbc.xa.client.OracleXADataSource"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:#db:1521:db"
username="myuser"
password="mypass"
maxActive="20"
maxIdle="30"
maxWait="-1"
validationQuery="SELECT 1 FROM DUAL"
/>
context.xml (tried in conf and app-file)
<ResourceLink global='jdbc/jKgvDS'
name='jdbc/jKgvDS' type="javax.sql.XADatasource"
auth="Container" />
persistence.xml
<persistence-unit name="jKgvPU" transaction-type="JTA">
<jta-data-source>jdbc/jKgvDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<validation-mode>NONE</validation-mode>
<properties>
<property
name="openjpa.jdbc.DBDictionary"
value="org.apache.openjpa.jdbc.sql.OracleDictionary" />
<property
name="openjpa.jdbc.DBDictionary"
value="oracle(maxEmbeddedBlobSize=-1,maxEmbeddedClobSize=-1)" />
<property
name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
Here we go (yeah...):
<Resource id="jKgvDS" name="jdbc/jKgvDS"
jtaManaged="true"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
url="jdbc:oracle:thin:#POSDBORA:1521:posdbora"
username="myuser"
password="mypass"
maxActive="20"
maxIdle="30"
maxWait="-1"
validationQuery="SELECT 1 FROM DUAL"
/>
References in web.xml or context.xml are not needed to run.

AssertionFailure: Transaction MARKED_FOR_JOINED after isOpen() call

While deploying in tomcat 8 server, I got following error
AssertionFailure: Transaction MARKED_FOR_JOINED after isOpen() call
Following are the details of the configuration.
Server: tomcat 8
Hibernate 3
Spring 3
Context.xml of server..
I have done database setting:
<Resource name="jdbc/meerkatDataSource"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:#10.237.59.104:1521:MEERKAT"
username="meerkat"
password="meerkat"
maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000"/>
<Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction"
factory = "org.objectweb.jotm.UserTransactionFactory"/>
<Transaction factory="org.objectweb.jotm.UserTransactionFactory" jotm.timeout="60"/>
<Resource
name="TransactionSynchronizationRegistry"
auth="Container"
type="javax.transaction.TransactionSynchronizationRegistry"
factory="org.objectweb.jotm.TransactionSynchronizationRegistryFactory"/>
Transaction.xml -->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransactionName" value="java:comp/UserTransaction">
<property name="transactionSynchronizationRegistryName" value="java:comp/env/TransactionSynchronizationRegistry"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
Persistent.xml-->
<persistence-unit name="meerkatPersistenceUnit" transaction-type="JTA">
<jta-data-source>jdbc/meerkatDataSource</jta-data-source>
Please suggest if any changes to be done. Also note that, database setting is strictly under tomcat server.
As per my understanding ,
here you are using data source type="javax.sql.DataSource" using the factory : "org.apache.tomcat.jdbc.pool.DataSourceFactory"
and the transaction manager is jta type :
class="org.springframework.transaction.jta.JtaTransactionManager">
It can be solved if it the data bas is replaced by having the type
javax.sql.XADataSource in turn using the class :
an org.enhydra.jdbc.pool.StandardXAPoolDataSource. Make sure the prsistence manager uses the same datasource.
For further info go through :
https://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html
http://xapool.ow2.org/doc/api/jdoc-1.3/org/enhydra/jdbc/pool/StandardXAPoolDataSource.html

Tomcat 8, axis2 webservices aar, spring jndi not bound in context

I have been working on an issue where JNDI name is not found in the context an axis2 webservice is running. This issue is only in Tomcat 8 when I use spring.
Some details: (I will provide the elements that are relevant)
1. services.xml
<service name="ScoreService" class="com.bpl.ws.service.ScoreServiceInitializer">
<description>Simple test service</description>
<parameter name="ServiceObjectSupplier" locked="false">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter>
<parameter name="SpringBeanName" locked="false">scoreService</parameter>
2. server.xml:
<Host name="localhost" appBase="webapps" xmlBase="C:\Applications\apache-tomcat-8.0.30-windows-x64\context"
unpackWARs="true" autoDeploy="true">
Context.xml
<JarResources className="org.apache.catalina.webresources.DirResourceSet"
base="C:\Applications\apache-tomcat-8.0.30-windows-x64\commonLib" webAppMount="/WEB-INF/lib"/>
4.JNDI resource in the context file:
<Resource name="jdbc/ADS" auth="Container"
factory="com.bpl.ws.EncryptedJdbcDataSourceFactory"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#xxxxxxx.com:3203/xxxx"
username="xxxxx"
password="xxxx"
initialSize="10"
logAbandoned="false"
maxActive="20"
maxIdle="10"
maxWait="10000"
removeAbandoned="true"
removeAbandonedTimeout="120"
jdbcInterceptors="QueryTimeoutInterceptor(queryTimeout=10)"
testOnBorrow="true"
validationInterval="30000"
validationQuery="Select 1 from dual"/>
As shown in services.xml file listing, I am using an initializer class and the code to load the spring context looks like this:
4.ScoreSerivceInitializer
public void startUp(ConfigurationContext ignore, AxisService service) {
System.out.println("SCORESERVICE:: Starting up..");
DataSource ds;
ClassLoader cloader = service.getClassLoader();
Thread.currentThread().setContextClassLoader(cloader);
System.out.println("SCORESERVICE:: spring context starting up");
spContext = new ClassPathXmlApplicationContext(new String[] {"DST-Context.xml"},false);
spContext.setClassLoader(cloader);
try {
spContext.refresh();
spring context.xml (DST-Context.xml)
The DST-Context.xml entry looks like this:
<bean id="applicationContext"
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean id="datasource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/ADS</value>
</property>
</bean>
Tomcat log:
[WARN] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasource' defined in class path resource [DST-Context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/ADS] is not bound in this Context. Unable to find [java:comp].org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasource' defined in class path resource [DST-Context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException:
Name [java:comp/env/jdbc/ADS] is not bound in this Context. Unable to find [java:comp].
Without changing anything in the configuration, if I changed the ScoreServiceInitializer to do this:
initCtx = new InitialContext();
envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource)
envCtx.lookup("jdbc/ADS");
Everything works. As you see here I don't use any spring and the jndi datasource is in context.
If I deploy the code with spring config in Tomcat 7, it works fine.
The spring context seems to be the issue but have been looking at it for a while and cant seem to figure out why Tomcat 8 has a different behavior compared to Tomcat 7. I know that Tomcat8 has changed some behavior in terms of how the Resources are configured and the dbcp is now dbcp2 and I have updated config files accordingly.
Any help is greatly appreciated. Please let me know if any other info is required.
#bplso I had the same issue when I upgrade tomcat7 to tomcat8.5 on a axis2 project, try the steps given in image. It may be solve your issue.
!
Step 1: Specify the connection resource in server.xml inside <GlobalNamingResources> like
<Resource name="jdbc/name"
global="jdbc/name"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
auth="Container"
type="javax.sql.DataSource"
username="xxx"
password="YYY"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://host:port/dbname"
maxTotal="10"
maxIdle="10"
maxWaitMillis="10000" />
Step 2: Specify the resource reference in web.xml inside the <web-app> like
<resource-ref>
<description> This is a reference to the global Resource for SQL database connetion. </description>
<res-ref-name>jdbc/name</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Step 3: Create a context.xml file and paste it in your project/META-INF/ path as below
<?xml version=.1.0 encoding="UTF-8.?>
<Context>
<ResourceLink name="jdbc/name" global="jdbc/name" type="javax.sgl.DataSource"
</Context>

JNDI DataSource: migrating from Tomcat to jBoss/Wildfly

I have a web application that uses Tomcat 7, Spring MVC 4.0, and JPA (Hibernate implementation). I am migrating this application to jBoss/Wildfly application server.
Currently, the DataSource is injected in the application with JNDI in a Spring configuration file:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MY_DB" expected-type="javax.sql.DataSource" />
The data source itself is defined in $CATALINA_HOME/conf/context.xml in the following way:
<Context>
<Resource name="jdbc/MY_DB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/MY_DB?useUnicode=yes&characterEncoding=UTF-8"
username="user1"
password=""
validationQuery="select 1"
removeAbandoned="true"
removeAbandonedTimeout="120"
maxWait="60"
maxActive="20"
maxIdle="10" />
</Context>
How can I define this JNDI DataSource in JBoss/Wildfly?
First, you must make the JDBC driver available to the application server; then you can configure the data source itself.
See more details in Data Source Configuration in AS 7 and DataSource configuration

Spring DataSource configuration using JNDI

I am having trouble configuring the dataSource bean using JNDI in the Spring applicationContext.xml file.
This is how my applicationContext.xml entry looks like:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myApp" expected-type="javax.sql.DataSource"/>
I have made an entry in web.xml:
<resource-env-ref>
<resource-env-ref-name>jdbc/myApp</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
<resource-env-ref>
Context.xml file in Tomcat:
<Resource
auth="Container"
factory="bitronix.tm.resource.ResourceObjectFactory"
name="jdbc/myApp" type="javax.sql.DataSource"
uniqueName="jdbc/myApp" />
With these configuration I keep getting this error on Tomcat console:
Caused by: javax.naming.NameNotFoundException: Name [jdbc/myApp] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:819)
at org.apache.naming.NamingContext.lookup(NamingContext.java:167)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:156)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:104)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:231)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:217)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
Please help me resolve this issue. It appears to me that the bitronix transaction factory in Context.xml is causing the issue.
You need to use a different jndi name, because in Tomcat all configured entries and resources are placed in the java:comp/env portion of the JNDI namespace and you need to specify that, as well:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/myApp" expected-type="javax.sql.DataSource"/>

Resources