61767

Configuring a JTA datasource for JBoss 8 (WildFly)

Question:

So, I read several times that if you use a Java EE container, you do not need to add environment params to an InitialContext in order to be able to use JNDI.

So I tried this:

@Bean public DataSource dataSource() { JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup(); return jndiDataSourceLookup.getDataSource("java:global/ExpensesDataSource"); }

However, retrieving a datasource using JNDI like this gives me a NoInitialContextException, telling me to specify the environment params.

Now, okay, so seems I was wrong to think it would work so flawlessly, so I tried retrieving the datasource like this:

@Bean public DataSource dataSource() { Properties jndiProperties = new Properties(); jndiProperties.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099"); jndiProperties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); jndiProperties.put("java.naming.factory.url.pkgs", "org.jboss.naming.org.jnp.interfaces"); JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup(); jndiDataSourceLookup.setJndiEnvironment(jndiProperties); return jndiDataSourceLookup.getDataSource("java:global/ExpensesDataSource"); }

However this gives me a javax.naming.CommunicationException: Failed to connect to server localhost:1099

I've also tried just using localhost:1099 or localhost, none of them worked.

So my question is: do I even need to specify these properties, since JBoss 8 is a Java EE container to my knowledge. And if so, what provider url do I need to specify here?

Answer1:

If you use javax.naming.InitialContext, you don't need to specify environment params, just like you said. For example:

InitialContext ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:jboss/datasources/ExampleDS");

Not sure how JndiDataSourceLookup works..

For looking up a datasource, you can inject it using @Resource (inside an ejb context)

@Resource(name= "java:jboss/datasources/ExampleDS") private Datasource ds;

Hope it helps!

Recommend

  • Testing the existence of a method
  • how to get connection string without opening a connection in java?
  • How do I configure HikariCP for postgresql?
  • How to add header columns based on data fetch from the database in gridview
  • Stored Function - Sending/Receiving Boolean - BD
  • cakephp get schema for list of tables
  • Should I install npm modules for Meteor in /usr/lib or /usr/local?
  • Facebook comments, for each page
  • How to read image field from MSSQL with PHP
  • Can I include a Generic type parameter in a lamba expression? (VB.NET 2010)
  • Unable to retrieve full text of title from JPEG metadata
  • what happens to finally block in the following cases?
  • HBase REST API Locking Rows
  • Python Floating Point Formatting
  • How does std::vector copy objects to its internal storage
  • django - make datetimefield accept unix timestamp
  • Compact framework voice recognize API or library
  • AutoHotKey - how to send control and same key multiple times
  • Bind drop down list with Dictionary
  • Why does Apple use assign rather than weak to store a delegate?
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • retrieving data from url in iphone
  • Default parameter as generic type
  • custom UITableViewCell with image for highlighting
  • MongoDb aggregation
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • How to create a file in java without a extension
  • Meteor: Do Something On Email Verification Confirmation
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Cross-Platform Protobuf Serialization
  • Where to put my custom functions in Wordpress?
  • RestKit - RKRequestDelegate does not exist
  • How to format a variable of double type
  • WPF Applying a trigger on binding failure
  • coudnt use logback because of log4j
  • JaxB to read class hierarchy