I am presented with the following situation: I have a web project that I am going to deploy on a jboss eap 7.1 server, on the server I have created a datasource to connect to the database, which works correctly; the tables and views that I have to modify were not created with primary keys, so doing a mapping with JPA is not possible, which is why I have thought of making native sql statements from a datasource, however, from what I have investigated, on the internet , the examples always put the connection data in the java code, my question is how can I get a connection to the database from the datasource configured on the server? the database is an oracle 12c
TL;DR: To access resources defined on a JEE server, use JNDI.
Although the concepts are not complicated, the technical details have their details and it's been a long time since I have had to define these things. In any case, I think that (since there are no other answers) it is better to give you an approximate answer that may fail in some detail than not that there is no answer.
As I mentioned, your Datasource will be defined in the JNDI. It will normally be inside the namespace
java:
(egjava:miDataSource
) orjava:jboss/
1 . Through the server management console you can check the JNDI elements.To get the data, two options:
JNDI pure and simple.
(This is also good for accessing any JNDI resource)
When you deploy/create the datasource on the server, it is assigned a JNDI name and is accessible through this service:
The name issue is somewhat messy; If I remember correctly you can directly access the "global" name (
java:
) but that can cause problems; If you have two applications that use the same name for Datasources that are different, you will not be able to have them on the same server.So in your application/webapp, what you do is indicate which Datasource you use and give the application a "local" name; for example in the
jboss-web.xml
:This creates a JNDI entry for you,
java:comp/env/jdbc/miDataSourceLocal
which is the one you use in your code:@Resource
In server-managed instances (which you don't make
new
, but the server creates, like e.g. Servlets and EJBs), you can use@Resource
and let the server inject the Datasource:This just automates your access to the JNDI, so the part about managing names to avoid collisions applies here as well.
1 If I remember correctly, JBoss forces you to do this.
after searching and searching i find what i needed in this link http://www.javapractices.com/topic/TopicAction.do?Id=127