29
Mar
17

JNDI lookup for a datasource in JBOSS


This page describes how to perform a jndi lookup in JBoss EAP 7. In the past the lookup string was always a confusing mess. Do we use “java/jdbc/dsName” or “java:comp/env” etc… With JBoss EAP 7 its simplified.

If you have a datasource defined in jboss as: java:/test

Example:

                <datasource jta="true" jndi-name="java:/test" pool-name="Test" enabled="true" use-ccm="true">
                    <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
                    <driver-class>org.mariadb.jdbc.Driver</driver-class>
                    <driver>mariadb</driver>
                </datasource>

Getting the datasource is as simple as writing the following anywhere in your application.

	public DataSource getDataSource() throws Exception {
		return (DataSource) (new InitialContext()).lookup("test");
	}
Advertisements

0 Responses to “JNDI lookup for a datasource in JBOSS”



  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 76 other followers

March 2017
S M T W T F S
« Mar    
 1234
567891011
12131415161718
19202122232425
262728293031  

Blog Stats

  • 818,144 hits

%d bloggers like this: