Getting OracleConnection on JBoss Wildfly 8.1 - java

I am trying to get to the underlaying Oracle connection in my WebApp on a JBoss Wildly 8.1 server.
I get an exception when I try to do the unwrap:
connection.unwrap(OracleConnection.class);
Throws
java.sql.SQLException: Not a wrapper for: oracle.jdbc.OracleConnection
The connection's class turns out to be com.sun.proxy.$Proxy37
This is my configuration in standalone.xml:
<subsystem xmlns="urn:jboss:domain:datasources:2.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/myds" pool-name="MyPool" enabled="true">
<connection-url>jdbc:oracle:thin:#//host:152x/blabla</connection-url>
<driver>Oracle11g</driver>
<security>
<user-name>xxx</user-name>
<password>yyy</password>
</security>
</datasource>
<drivers>
<driver name="Oracle11g" module="com.oracle.ojdbc6">
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
</driver>
</drivers>
</datasources>
</subsystem>
How can I get to the OracleConnection?
Edit: I have configured the oracle driver in the standalone.xml and the driver is added in the modules folder. In order to be able to get my code compiled, I have added a dependency in my pom-file to the driver as well. No idea if this is part of the cause.
Thanks

I found the solution!
When using this statement I can get to the OracleConnection object:
(oracle.jdbc.driver.OracleConnection) conn.getMetaData().getConnection();

Related

Not able to create mysql datasource in Jboss 7.1.1

I am using jboss as 7.1.1 final release. I am trying to add mysql driver to the server via the admin console. I am able to do so but when I go to create the datasources, I do not find the driver listed there. I followed the steps to add the driver to the server as mentioned in this link :
http://www.appeon.com/support/documents/appeon_online_help/1.5/server_configuration_guide_for_j2ee/ch03s03s03.html#d0e4128
I followed from step 3 of Installing a JDBC driver via the Web console as I had already created one management user.To summarize what I did,
I added a driver file E:\DevSoftwares\servers\jboss\jboss-as-7.1.1.Final\modules\com\mysql\main\META-INF\services\java.sql.Driver and its content as following : com.mysql.jdbc.jdbc2.optional.MysqlXADataSource(fully qualified driver class name).
create module.xml file inside E:\DevSoftwares\servers\jboss\jboss-as-7.1.1.Final\modules\com\mysql\main\ and its content as below :
Added mysql-connector-java-5.0.8-bin.jar file inside E:\DevSoftwares\servers\jboss\jboss-as-7.1.1.Final\modules\com\mysql\main\
Ran jar -uf mysql-connector-java-5.0.8-bin.jar META-INF\services\java.sql.Driver command to modify the jar.
After running this command I could see a services\java.sql.Driver file with driver class name as given in step 1 inside META-INF folder of the jar file which was not there before running the command.
I picked up the fully qualified name of the driver class for mysql for Jboss 7.x from below link :
http://www.appeon.com/support/documents/appeon_online_help/1.5/server_configuration_guide_for_j2ee/ch03s03s07.html#d0e5769
I was able to figure out what is causing the problem, when I try to enable the deployed jar I get an exception in the server console stating : unable to instantiate driver class com.mysql.jdbc.jdbc2.optional.MysqlXADataSource :
java/lang.ClassCastException :
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource.
However I checked the mysql-connector-java-5.0.8-bin.jar and was able to find the driver class mentioned above in the same directory structure as the classes fully qualified name.
I am not able to figure out why I am getting this exception or what I am doing wrong during the setup that is causing this issue.The platform I am using is windows. Could some one please help me with it.
NOTE : the content of module.xml
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.0.8-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>`
Please check that you have added your MySQL datasource configuration in standalone.xml or in equivalent XML file:-
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jndi-name="java:jboss/datasources/MySqlExampleDS" pool-name="MySqlExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/jboss7db</connection-url>
<driver>mysql</driver>
<pool>
<max-pool-size>30</max-pool-size>
</pool>
<security>
<user-name>root</user-name>
<password>admin</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>

How does JBoss 7.1.1 manage connection to different schemas of the same database

So I have a servlet where I login to a database with Datasource(where I only pass the JNDI name) with a default schema(which was set in the JBoss management console) but later on I need to connect on the same database with another schema in order to get some texts.
How does JBoss manage this ? Can I provide the later schema and password somehow in the java code ?
În standalone.xml you declare all your datasources. Those connections can be picked up by jndi at runtime by looking up on an InitialContext instance.
When using JNDI to form connections, you will need to configure the new datasource within the management console or the standalone.xml file. This will simply be a new datasource, with a connection URL going to the same database, but pointing to a new schema.
Example output in the standalone.xml:
<datasource jta="false" jndi-name="java:/firstDS" pool-name="firstDS" enabled="true" use-ccm="false">
<connection-url>jdbc:oracle:thin:#devdb:1521:SCHEMA_1</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver>oracle</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
...
<datasource jta="false" jndi-name="java:/secondDS" pool-name="secondDS" enabled="true" use-ccm="false">
<connection-url>jdbc:oracle:thin:#devdb:1521:SCHEMA_2</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver>oracle</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
Now in the second part of your application you will just refer to that second datasource's JNDI name when forming the connection.

Once we've configured datasource in JBoss AS should we add the JDBCDriver to dependencies in our maven project?

I'm working on a web project using maven, AppServer: JBoss AS 7. I'd like to clarify if we should add JDBCDriver into maven dependencies after I've configured my datasource configuration? I suspect it would be superflous, because JBoss AS is already used driver configured within the module.xml file as follows
<datasources>
<datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS">
<connection-url>jdbc:mysql://localhost:3306/</connection-url>
<driver>com.mysql</driver>
<!-- something else -->
</datasource>
<datasources>
<drivers>
<driver name="com.mysql" module="com.mysql">
</driver>
</drivers>
, but I don't know for sure. Couldn't you explain it to me?
No there is no need to add it in pom.xml because you have directly added jdbcDriver.jar for JBossAS. Adding mysql-connector-java in pom.xml will make sense if you would be using jboss-as-maven-plugin to deploy the mysql datasource

JBoss / WildFly connection pooling and closed connections

I have some applications running on JBoss 4.2.2, JBoss 5.1 and WildFly 8.1.0. All these applications use connection pooling. My datasources are something like that:
<datasource jta="false" jndi-name="java:/datasource/myawesomeds" pool-name="MyAwesomeDS" enabled="true" use-ccm="false">
<connection-url>jdbc:oracle:thin:#myserver.example.com:1521:oracle_service</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver>ojdbc6.jar</driver>
<security>
<user-name>username</user-name>
<password>MyPassWord</password>
</security>
<validation>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
As you can see, I use Oracle Database. The server version is 10g. When Oracle DB going down for a few minutes all my applications on JBoss and WildFly start showing SQLException: Closed Connection.
My question: Is it normal? Why JBoss and WildFly don't check the connections to verify this?
Yes, this is normal.
You can however define that wildfly/jboss should verify the connection when fetching it from the connection pool. All your connections will be invalid, so normally it should create a new connection for you if you defined a minimum number of connections that should be present.

jboss-eap-6.1 oracle driver definition when installed as deployment

I have several Jboss EAP 6.1 installations working with Oracle driver installed as a module.
This is the standard configuration I use in standalone.xml:
<datasource jndi-name="java:jboss/fooDatasource" pool-name="java:jboss/fooDatasource" enabled="true" use-java-context="false" >
<connection-url>jdbc:oracle:thin:#1.2.3.4:1527/SOMEDB.foo</connection-url>
<driver>oracle</driver>
<security>
<user-name>xxxxx</user-name>
<password>xxxxxxxxx</password>
</security>
[...]
</datasource>
<driver name="oracle" module="oracle.jdbc">
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
<datasource-class>oracle.jdbc.OracleDriver</datasource-class>
</driver>
The ojdbc6.jar is in $JBOSS_HOME/modules/system/layers/base/oracle/jdbc/main/ together with the appropriate module.xml and everything works fine.
Now a customer required to install the driver as a deployment, so I moved ojdbc6.jar to $JBOSS_HOME/standalone/deployments/ and I see from logs that it is deployed without errors:
[org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-3) JBAS010403: Deploying JDBC-compliant driver class oracle.jdbc.OracleDriver (version 11.2)
INFO [org.jboss.as.server] (ServerService Thread Pool -- 25) JBAS018559: Deployed "ojdbc6.jar" (runtime-name : "ojdbc6.jar")
But I don't know how to edit my standalone.xml to make it work again: i tried to edit the driver definition "module" attribute with several different values (ojdbc6.jar, deployment.ojdbc6.jar, oracle.jdbc.OracleDriver...) but none seem to "match" and Jboss keeps throwing errors at startup:
ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 24) JBAS014613: Operation ("add") failed - address: ([
("subsystem" => "datasources"),
("jdbc-driver" => "oracle")
]) - failure description: "JBAS010441: Failed to load module for driver [ojdbc6.jar]"
[...]
INFO [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report
JBAS014775: New missing/unsatisfied dependencies:
service jboss.jdbc-driver.oracle (missing) dependents: [service jboss.driver-demander.java:jboss/spiDatasource, service jboss.data-source.java:jboss/fooDatasource]
Could anyone please provide a working example of the driver definition?
Thanks
Ok, I found the answer myself.
Surprisingly, all the guides I found around explain how to do this configuration via web admin interface or via jboss-cli, but no one in the Jboss community seem to bother explaining how to manually edit the standalone.xml to do the job.
This is a working example (basically I just deleted the entire Oracle driver definition section and replaced the driver name in the datasource definition with the name of the runtime name of the deployed jar file):
<datasource jta="false" jndi-name="java:/jdbc/fooDS" pool-name="foo-ds" use-ccm="false">
<connection-url>jdbc:oracle:thin:#1.2.3.4:1527/SOMEDB.foo</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver>ojdbc6.jar</driver>
[...] other datasource stuff here
</datasource>
# DELETE FROM HERE...
<driver name="oracle" module="oracle.jdbc">
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
<datasource-class>oracle.jdbc.OracleDriver</datasource-class>
</driver>
# ...TO HERE
That's all.
Probably you have to mention in this way...
<subsystem xmlns="urn:jboss:domain:datasources:1.1">
<datasources>
<datasource jndi-name="java:jboss/XXX" pool-name="XXX" enabled="true" use-java-context="true">
<connection-url>jdbc:oracle:thin:#SID:PORT:DBNAME</connection-url>
<driver>oracle</driver>
<security>
<user-name>user</user-name>
<password>password</password>
</security>
</datasource>
<drivers>
<driver name="oracle" module="com.oracle">
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
<datasource-class>oracle.jdbc.OracleDriver</datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
Create directories like x1/x2/main
Create module.xml file under main directory
Keep ojdbc6-11.1.1.3.0.jar in main directory level
In Module.xml
<module xmlns="urn:jboss:module:1.1" name="x1.x2">
<properties>
<property name="jboss.api" value="unsupported"/>
</properties>
<resources>
<resource-root path="ojdbc6-11.1.1.3.0.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
In domain.xml
<datasource jndi-name="java:/TestName" pool-name="TestName" enabled="true" use-java-context="true">
<connection-url>jdbc:oracle:thin:#ldap://xxxxx:3000/UKXXX,cn=OracleContext,dc=dcm,dc=XXXX</connection-url>
<driver>dps</driver>
<security>
<user-name>XXXXX</user-name>
<password>*****</password>
</security>
</datasource>
<drivers>
<driver name="dps" module="x1.x2">
<xa-datasource-class>oracle.jdbc.driver.OracleDriver</xa-datasource-class>
</driver>
</driver>
</drivers>
Try to keep the correct ojdbc jar, some versions won't work :)

Categories

Resources