JBoss datasource creation - java

JBoss creates datasource from *ds.xml files and I want to do this without such an xml file and still make JBoss provide datasources through jndi.
The code that I have written registers 4 mbeans :
RARDeployment at jboss.jca:service=ManagedConnectionFactory,name=" + dataSourceJndiUrl
JBossManagedConnectionPool at "jca:service=ManagedConnectionPool,name=" + dataSourceJndiUrl
TxConnectionManager at "jboss.jca:service=XATxCM,name=" + dataSourceJndiUrl
WrapperDataSourceService at "jboss.jca:service=DataSourceBinding,name=" + dataSourceJndiUrl
I would like to be able to retrieve a DataSource objects using the following code:
InitialContext ctx = null;
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(dataSourceJndiUrl);
Can someone tell me if the above is a correct approach and if yes what else needs to be done in order for it to work?

yes, is possible and is the best way to do it.
In my case, I needed to use an standalone-full configuration (EAP 6.2).
The first you need is the correct JDBC for your DB, for example I used the ojdbc6.jar. Copy this Jar into ${JBOSS_HOME}/modules/com/oracle/main/ . The JDBC must be with an module.xml, to make it valid for JBoss. To me, this was a correct configuration of the module.xml:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.oracle">
<resources>
<resource-root path="ojdbc6.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
Then, on the standalone-full.xml file you will find a tag with the urn of datasources, inside of that tag a datasources tag, and inside a drivers tag. You need to add this new JDBC, to me this was a succesfull configuration:
<drivers>
<driver name="Oracle" module="com.oracle">
<xa-datasource-class>com.oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
</driver>
</drivers>
Start the JBoss with that configuration and go to the Profile > Connector > Datasources. Add a new one with your own configuration (JNDI, User, Password, etc...), dont forget to add on properties the URL to your DB. Then, you will be able to use the JNDI for the DataSource.

Related

Different resources from context.xml for maven profiles

I have an application running on a tomcat 8 which is working fine. Now I want to build the application for a development, a test and a production environment where the application connects to different database servers and I would like to select which resource to use by a maven property set in the different profiles.
So in my context.xml I have defined my data sources looking like this for each DB server.
<Resource name="jdbc/db-dev" auth="Container"
type="javax.sql.DataSource" maxTotal="1000" maxIdle="30"
maxWaitMillis="10000" username="user" password="password"
driverClassName="org.mariadb.jdbc.Driver"
url="jdbc:mariadb://x.x.x.x:3306/dbname;" />
<Resource name="jdbc/db-test" auth="Container"
type="javax.sql.DataSource" maxTotal="1000" maxIdle="30"
maxWaitMillis="10000" username="user" password="password"
driverClassName="org.mariadb.jdbc.Driver"
url="jdbc:mariadb://x.x.x.x:3306/dbname;" />
And in my web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>${db.context}</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And in my pom.xml
<profile>
<id>dev</id>
<properties>
<db.context>jdbc/db-dev</db.context>
</properties>
</profile>
And in Java
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup(ENV);
DataSource ds = (DataSource) envContext.lookup("jdbc/db"); // jdbc/db-??? is the issue
return ds.getConnection();
} catch (NamingException e) {
throw new SQLException(e);
}
And there's the problem, in Java I have a fixed string value pointing to only one resource. I know I could create a properties file during build, but for only that one value it seems a little to much. Is there a way to have the resource-ref point to different resources from the context xml while having only one reference name?
Another method I found was to have different resource files which I can include and exclude probably per profile, haven't got into it that much yet but maybe there is a far more easy solution which I just don't see right now.
I would prefer to have the same binaries (i.e. WAR in this case) for every environments in order to follow "Build one, deploy often" CI/CD principle. The environment dependent configurations will then be injected in some ways such as through environment variables ,JVM options when starting the application or external configuration file etc.
From this , Tomcat supports using ${xxxx} for variable substitution in their configuration file :
Tomcat configuration files are formatted as schemaless XML; elements
and attributes are case-sensitive. Apache Ant-style variable
substitution is supported; a system property with the name propname
may be used in a configuration file using the syntax ${propname}. All
system properties are available including those set using the -D
syntax, those automatically made available by the JVM and those
configured in the $CATALINA_BASE/conf/catalina.properties file.
So, context.xml will probably looks likes:
<Resource name="jdbc/db" auth="Container"
type="javax.sql.DataSource" maxTotal="1000" maxIdle="30"
maxWaitMillis="10000" username="${DB_USER}" password="${DB_PASSWORD}"
driverClassName="org.mariadb.jdbc.Driver"
url="jdbc:mariadb://${DB_HOST}/dbname;" />
Then configure ${DB_USER} , ${DB_PASSWORD} and ${DB_HOST} as an environment variables or JVM options when starting tomcat etc....

How to load properties file in JBOSS 6 from other place

I want to change location of all properties file but I do not know where to change in JBOSS 6 for new path, I am asking it because I am new in JBOSS.
it uses some default path, for example for "com/test/configuration/main" path is modules/com/test/configuration/main
but I want to load all property from say c:\prop_files\
for it i have tried to modified module.xml
<module xmlns="urn:jboss:module:1.1" name="com.test.configuration">
<resources>
<resource-root path="c:\props_files\"/>
</resources>
</module>
but it is giving me exception
org.jboss.modules.xml.XmlPullParserException: Failed to add resource root 'C:\props_files' at path 'C:\props_files'
please suggest.
Firstly, you need to add the path in your configuration file ( standalone.xml/domain.xml).
For example in standalone.xml (it goes after </extensions> and before <management> tag):
<paths>
<path name="my.home.dir" path="C:\Users\elvis\Downloads"/>
</paths>
You can do that via CLI as well:
./bin/jboss-cli.sh --connect --controller=localhost:9999
/path=my.home.dir:add(path=C:\\Users\\elvis\\Downloads)
Then you reference the path in your application, for example:
String path = System.getProperty("my.home.dir") + propertiesFileName;
Properties props = new Properties();
URL url = new URL(path);
props.load(url.openStream());

Wildfly 9 failing to load MySQL driver on startup

I am creating a web application for WildFly, which will connect to a MySQL database through JPA (Hibernate). For now, I am just trying to get WildFly to start up and load the MySQL driver in standalone mode. I am using this page as a guide: http://wildfly.org/news/2014/02/06/GlassFish-to-WildFly-migration/
I am running WildFly and MySQL locally on a Windows system:
Windows 7 Enterprise SP1
Oracle Java SE 1.8.0_45
WildFly 9.0.0.Final
MySQL Server 5.6
Attempts to use the recommended console commands did not succeed, so I have manually edited to WildFly configuration files to look like those in the examples on the page linked above. First, I created the module directory and placed in it the MySQL connector JAR and module.xml file:
Directory of C:\wildfly-9.0.0.Final\modules\system\layers\base\com\mysql\main
07/06/2015 09:54 AM <DIR> .
07/06/2015 09:54 AM <DIR> ..
07/06/2015 10:12 AM 334 module.xml
07/01/2015 02:38 PM 968,668 mysql-connector-java-5.1.35.jar
The above connector jar was copied from my local Maven repository, which Maven obtained through the following dependency:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
The module.xml file was manually edited as follows, to resemble the example I found on wildfly.org:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.35-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
Finally, I added the driver and datasource to the datasources section of standalone.xml:
<datasource jndi-name="java:/MySQLDS" pool-name="MyDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>secret</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">
<driver-class>com.mysql.jdbc.Driver</driver-class>
</driver>
</drivers>
Upon starting WildFly in standalone mode, by running %WILDFLY_HOME%\bin\standalone.bat, the following is the first error listed in %WILDFLY_HOME%\standalone\logs\server.log:
2015-07-06 10:25:47,321 ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 33) WFLYCTL0013: Operation ("add") failed - address: ([
("subsystem" => "datasources"),
("jdbc-driver" => "mysql")
]) - failure description: "WFLYJCA0041: Failed to load module for driver [com.mysql]"
Similar issues that I have seen posted on Stack Overflow and other question/answer sites usually point to an oversight such as a typo in config files or a misnamed file. However, I've been over this over and over and cannot see any such mistake, and the same error has occurred even after upgrading from Java SE 7 and WildFly 8.2 and re-creating the configuration files from scratch. Any assistance would be greatly appreciated.
In my case it was a matter of having the wrong user:group for the directories and files under ../com/mysql/main
I changed it to wildlfy and everything worked as expected.
There was a typo in module.xml. The name of the connector JAR listed in module.xml did not match the actual JAR file in modules\system\layers\base\com\mysql\main.
In my case, it was an issue with double quotes. When I copied a WordPress sample of module.xml, I got (note the curly quotes):
<resource-root path=”postgresql-9.4.1211.jar”/>
... but Wildfly is very picky and needs this (straight quotes):
<resource-root path="postgresql-9.4.1211.jar"/>
In my case I have missed the main folder which module.xml and connector jar file need to be included.
Earlier it was
JBOSS_HOME\modules\system\layers\base\com\mysql\module.xml
and the correct path should be,(module.xml and jar needed be included inside of main)
JBOSS_HOME\modules\system\layers\base\com\mysql\main\module.xml
I know it has been while but that may help for others. The recommended approach is to deploy a driver is using the wildfly console (localhost:9990/console). once you deployed the driver jar then create your DS again by using the console then it will create it automatically in the standalone.xml. Sometimes, when doing those stuffs manually may cause missing tiny details which drive us crazy.
For me, it seems to be a coding problem. Before the tag
<module>
...
</module>
in my module.xml file, there is a mysterious blank, which is not utf-8 or English, causing my failure. After I deleted the blank or changed it to an English one. Everything is ok. Also, I looked for some solutions to this problem. Most of them can be summed to the module.xml file's problem. A name typo or the content coding. Hope it heplful for others.
Maybe two solutions for your issue:
You forgot the attribute slot on module element in module.xml file,
e.g.:
<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.3" name="org.postgresql" slot="main">
<resources>
<resource-root path="postgresql-9.4.1212.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
add code slot="main" in module element, it will take effect.
If above approach doesn't work, it is considered with XML Module descriptors . The same issue with Wildfly Failed to Load Module for Oracle Driver:
Change the namespace version of module element in module.xml.
enjoy it.

Need help creating SQL Server datasource for JBoss EAP 6.1.0

I am working on a web service application on JBoss EAP 6.1.0, which requires a datasource for SQL Server 2008 to be created. I have downloaded and extracted the sqljdbc jar file, but am not sure where it goes on the application server. I tried referencing the EAP 6.1 configuration guide and a couple of other websites, but that did not help. I would be very grateful if someone could guide me with the steps to register the datasource on the application server.
It goes into your modules folder. You have have something like the following:
<JBOSS_HOME>\modules\com\microsoft\sqlserver\main
Your sqljdbc would be inside the folder structure above. You would also need to create a module.xml file in the same folder similar to:
<module xmlns="urn:jboss:module:1.1" name="com.microsoft.sqlserver">
<resources>
<resource-root path="sqljdbc4.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
Then finally in your standalone.xml, the datasources section should have a drivers definition that references your module as such:
<drivers>
<driver name="sqlserver" module="com.microsoft.sqlserver">
<xa-datasource-class>com.microsoft.sqlserver.jdbc.SQLServerXADataSource</xa-datasource-class>
</driver>
</drivers>
Hope that helps

set a data source in jboss standalone mode

I want to set a mysql data source in jboss standalone mode. I have already deploy the mysql-connector-java-5.1.15-bin.jar and set the below data source configuration in standalone.xml under datasources
<datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/testdb</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
</security>
</datasource>
And when i click on the configured data source name in the web console im getting below error,
Internal server error{
"outcome" => "failed",
"failure-description" => "JBAS014739: No handler for read resource at address [
(\"subsystem\"=>"\datasource\"),
(\"data-source\"=>"\MySqlDS\"),
(\"statstics\"=>"\pool\"),
"],
"roleback" => "true"
}
I didnt add any thing to the drivers section since it not nessaccary,
Below one is set to the sample data source set in jboss
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class
</driver>
</drivers>
What i am missing here please?
You will also need to specify driver class
<driver-class>com.mysql.jdbc.Driver</driver-class>
Checkout this link How do I migrate my application from AS5 or AS6 to AS7
I've experienced the same issue under the same circumstances. The problem was that my AS didn't have the required module for PostgreSQL. Check in jboss/modules/org if you have a folder named postgresql. If not then create it. Then create a directory in it named main.
You then have to have two files present in there:
PostgreSQL JDBC JAR
module.xml configuration file
Download the JAR file according to the database you're using and copy it here. As for the module.xml just create a new file and set up the configuration. Mine looks like this, customize it for your case:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="org.postgresql">
<resources>
<resource-root path="postgresql-9.3-1100.jdbc4.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
Basically you just have to change the resource path to where your JAR file is.
This is an issue with JBOSS, it doesn't warn you even though the JDBC driver is missing. I've wasted a ton of time finding this hidden bug :D

Categories

Resources