JNDI DataSource: migrating from Tomcat to jBoss/Wildfly - spring

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

Related

Application is not working in WebSphere

I deployed WAR file on Websphere console and mapped it to datasource. I am able to test the datasource which I configured with PostgreSQL server details. But my application is not connecting to the server. I am new to WebSphere and could anyone please help me to configure the datasource based on the below context.xml file. My application works well in tomcat but not in Websphere.
I think am doing something wrong in the datasource configuration.
<Resource
name="jdbc/domains/ABC" url="jdbc:postgresql://localhost:5432/postgres"
initConnectionSqls="SET search_path TO my_schme;"
username="abccc"
password="******"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
validationQuery="select 1"
initialSize="5"
maxActive="20"
maxIdle="10"
maxWait="-1"
/>
Here is an example based on the most common way to configure data sources in WebSphere Application Server Liberty -- in server.xml (it is also possible to define within the application via the standard #DataSourceDefinition annotation or deployment descriptor, which would be a separate example)
In server.xml,
<featureManager>
<feature>jdbc-4.2</feature>
<feature>jndi-1.0</feature>
... other features
</featureManager>
<dataSource id="ABC" jndiName="jdbc/domains/ABC" type="javax.sql.ConnectionPoolDataSource" validationTimeout="30s">
<jdbcDriver libraryRef="PostGreLib" javax.sql.ConnectionPoolDataSource="org.postgresql.ds.PGConnectionPoolDataSource"/>
<properties url="jdbc:postgresql://localhost:5432/postgres"/>
<containerAuthData user="abccc" password="******"/>
<connectionManager maxPoolSize="20" connectionTimeout="-1"/>
<!-- no equivalents for initialSize and maxIdle -->
<onConnect>SET search_path TO my_schme;</onConnect>
</dataSource>
<library id="PostGreLib">
<file name="C:/PostGreSQL/postgresql-42.1.4.jar"/>
</library>
Here are some helpful knowledge center articles on data source configuration in WebSphere Application Server Liberty,
https://www.ibm.com/support/knowledgecenter/en/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/twlp_dep_configuring_ds.html
https://www.ibm.com/support/knowledgecenter/en/SSEQTP_liberty/com.ibm.websphere.liberty.autogen.base.doc/ae/rwlp_feature_jdbc-4.2.html

Defining a datasource resource in tomcat context.xml

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

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>

How to set Oracle's fixedString property in Orion Application Server?

I want to set the fixedString=true connection property for the Oracle JDBC driver in the datasource definition in Orion Application Server 2.0.7 (latest stable release).
I've tried the following but didn't work:
<data-source
class="com.evermind.sql.DriverManagerDataSource"
name="datasource_name" location="jdbc/datasource_location"
connection-driver="oracle.jdbc.OracleDriver"
username="user" password="pass"
url="jdbc:oracle:thin:#//database_host:1521/XE" >
<property name="fixedString" value="true" />
</data-source>
Note: I've managed to set the property in Tomcat 6.0 this way (using the same Oracle JDBC driver, of course):
<Resource name="jdbc/datasource_name" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="user" password="pass" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#//database_host:1521/XE"
connectionProperties="fixedString=true;"
/>
But I need to set this in Orion. How can I do that?
It is a bug in the oracle application server you have to install OAS 10.1.2.3 has fix for that

Resources