JBoss DataSource configuration Issue - java

I am unable to configure datasource in jBoss. Following is configuration in jboss web.xml
<jboss-web>
<resource-ref>
<res-ref-name>jdbc/testDataSource</res-ref-name> <!-- matches web.xml -->
<jndi-name>java:jdbc/testDataSource</jndi-name> <!-- matches oracle-ds.xml -->
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
</jboss-web>
We have created oracle-ds.xml which contains:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/testDataSource</jndi-name>
<connection-url>jdbc:oracle:thin:#11.120.184.77:1521:starsdev</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>cmsusr</user-name>
<password>cmsusr</password>
<min-pool-size>2</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>5</idle-timeout-minutes>
<track-statements/>
<metadata>
<type-mapping>Oracle10g</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
I have created one servlet which contains following code in service method:
Context initialContext = new InitialContext();
Context envContext = (Context) initialContext.lookup( "java:comp/env" );
DataSource ds = (DataSource) envContext.lookup( "jdbc/testDataSource" );
PrintWriter pw = response.getWriter();
pw.print(ds);
When I'm running my servlet it is showing following error:
[STDOUT] javax.naming.NameNotFoundException: jdbc not bound

Do it in a single lookup. There's no need to use two Contexts here.
Context initialContext = new InitialContext();
DataSource ds = (DataSource) initialContext.lookup("java:comp/env/jdbc/testDataSource");

Related

Tomcat 8.5.4 javax.naming.NameNotFoundException

I am developing a web application with Tomcat 8.5.4. I'm using a properties configured as Resource. So, my $TOMCAT_HOME/conf/server.xml contains
<GlobalNamingResources>
<Resource name="properties/global" auth="Container" type="java.util.Properties" />
</GlobalNamingResources>
The META-INF/context.xml of my webapp has
<Context antiResourceLocking="true" path="/mywebapp">
<ResourceLink global="properties/global" name="properties/global" type="java.util.Properties"/>
</Context>
I'm looking up the resource as follow
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
(Properties) envContext.lookup("properties/global");
However, I'm getting the exception
javax.naming.NameNotFoundException: Name [properties/global] is not bound in this Context. Unable to find [properties].
What's wrong? Thanks.

Create JNDI Connection for Ingres Database

I have a Java project and I've been trying to create a JNDI connection for my Ingres database but have been unsuccessful. I'm not sure if there is something specific to ingres that needs to be included but after quite a bit of research I haven't been able to get things to work.
In my project I have my datasource info in the web.xml file and the context.xml
context.xml has the following info
<Context>
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.Datasource"
username="myUser" password="password" driverClassName="com.ingres.jdbc.IngresDriver"
url="databaseURL" maxActive="8" maxIdle="4" maxWait="100" />
</Context>
My web.xml has the following info
<web-app>
<resource-ref>
<description>Project Descrip</description>
<res-ref-name>jdbc/myDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
In my java code I'm trying to get my connection using the following four lines
Context initContext = new InitialContext();
Context envContext = (Context) initContent.lookup("java:comp/env");
Datasource ds = (DataSource) envContext.lookup("jdbc/myDB");
return ds.getConnection();
After the third line is executed I get an exception that says: NamingException - Cannot create resource instance
I have found dozens of posts with this same exception and have tried the suggested solutions with no luck. I'm using a Tomcat 7 server and have made sure to include the necessary ingres jar (iijdbc.jar) to my WEB-INF/lib folder and to my tomcat lib folder.
Any help or suggestions would be greatly appreciated
I don't know much about Ingres, but if attempting to make a datasource is similar to JBOSS and DB2 or MySQL. I noticed that in your WEB.XML you define your resource-ref, but you don't mention the anything about the servlet parameters.
<servlet>
<description>Servlet Description</description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>servlet.MyServlet</servlet-class>
<init-param>
<param-name>MyDB</param-name>
<param-value>java:comp/env/jdbc/MyDB</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Then you would need to code in your sevlet:
InitialContext initialContext = new InitialContext();
DataSource dataSource = (DataSource)initialContext.lookup(this.getInitParameter("MyDB"));
Hope this helps.
Can you try adding global="jdbc/myDB" attribute as in your Resource tag as follows:
<Context>
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.Datasource"
username="myUser" password="password" driverClassName="com.ingres.jdbc.IngresDriver"
url="databaseURL" maxActive="8" maxIdle="4" maxWait="100" global="jdbc/myDB"/>
</Context>

Java JNDI lookup failing on JBoss AS 6 from external Java program

I am upgrading from JBoss AS 4.0.4.GA to JBoss 6.0.0.Final.
I have been unable to lookup a HornetQ Topic from an external Java client, so I decided to write a little test Java program that did a simple lookup of a DataSource. That lookup is failing. I can see the DataSource in the web console and it has:
Pool JNDI Name = MyDS
I used to (with JBoss 4.0.4.GA) lookup the DataSource using "java:/MyDS", but that is not working. I have seen some talk about not being able to lookup objects (Queues,Topics,DataSources,EJBs,etc) from outside the AS. I'd be surprised if that is the case, but if it is, then how do I disable that "feature"?
The DS is in server/myconf/deploy/mysql-ds.xml:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>xxxxxxxx</user-name>
<password>xxxxxxxx</password>
<prepared-statement-cache-size>50</prepared-statement-cache-size>
<min-pool-size>20</min-pool-size>
<max-pool-size>40</max-pool-size>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
The Java program that looks up the DS is:
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(javax.naming.Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(javax.naming.Context.PROVIDER_URL, "jnp://localhost:1099");
InitialContext ctx = new InitialContext(env);
Object o = ctx.lookup("java:/MyDS");
The lookup line throws a NameNotFoundException:
javax.naming.NameNotFoundException: MyDS not bound
I've tried the lookup with just "MyDS" rather than "java:/MyDS", but it still fails. I don't think the problem is the name I'm using to lookup, rather something preventing me doing any lookup from an external application - and if that is the case, I need to enable such lookups.
Update the datasource definition like:
<use-java-context>false</use-java-context>
e.g.
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>xxxxxxxx</user-name>
<password>xxxxxxxx</password>
<prepared-statement-cache-size>50</prepared-statement-cache-size>
<min-pool-size>20</min-pool-size>
<max-pool-size>40</max-pool-size>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

NoInitialContextException

I have a problem.
I want to connect to database using JDBC, I have Tomcat server. For this I use connection pool.
According to Internet tutorials I've written:
context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Server" docBase="dbcp" debug="5"
reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container"
type="javax.sql.DataSource" removeAbandoned="true"
removeAbandonedTimeout="30" maxActive="100"
maxIdle="30" maxWait="10000" username="root"
password="newpass"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/delta_server"/>
</Context>
web.xml :
<resource-ref>
<description>DB Connection Pooling</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
and I try to connect...
Connection conn=null;
DataSource ds;
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
ds = (DataSource) envContext.lookup("jdbc/TestDB");
conn = ds.getConnection();
But I get a mistake:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
What to do???

Getting DataSource resource in a Java web app

I have the following resource tag in my context.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myApp">
<Resource name="jdbc/myDS" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="1000"
username="user" password="passwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDB" />
</Context>
I am developing a Java web app using the Stripes framework in NetBeans.
How can I get this resource from within a Java class?
You need your bean to be instantiated by something (a dependecy injection framework) which knows how to handle the #Resrouce annotation. JSP itself doesn't know how.
In this case it would be simpler to locate the DataSource in the JNDI context:
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/myDS");
Thank you Bozho for the answer. I only had to change the lookup string to get it to work:
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDS");
Connection conn = ds.getConnection();

Categories