-
1. Re: how to lookup datasource within module
nickarls May 17, 2013 4:56 AM (in response to haribo)1 of 1 people found this helpfulI know I've done it with earlier AS7 versions (datasource-based custom log appender) but I was warned that there was no gurarantee it was future-proof. But I would expect the error to come later on, are you sure the hashmap was passed along correctly? Tried the system property alternative?
-
2. Re: how to lookup datasource within module
haribo May 21, 2013 2:05 AM (in response to nickarls)Nicklas Karlsson schrieb:
...are you sure the hashmap was passed along correctly? Tried the system property alternative?
I did this here:
{code}
...
Properties properties = new Properties();
properties.put("java.naming.factory.initial", "org.jboss.as.naming.InitialContextFactory");
context = new InitialContext(properties);
...
{code}
surely adding org.jboss.as.naming to my module.xml
{code:xml}
<module xmlns="urn:jboss:module:1.1" name="de.haba.contextutil">
<resources>
<resource-root path="contextutil.jar"/>
</resources>
<dependencies>
<module name="javax.naming" />
<module name="org.jboss.as.naming" />
<module name="org.apache.log4j" />
</dependencies>
</module>
{code}
results in
{noformat}
08:01:09,053 ERROR [de.haba.context.ContextUtil] (http--0.0.0.0-8080-2) javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.as.naming.InitialContextFactory [Root exception is java.lang.ClassCastException: org.jboss.as.naming.InitialContextFactory cannot be cast to javax.naming.spi.InitialContextFactory]: javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.as.naming.InitialContextFactory [Root exception is java.lang.ClassCastException: org.jboss.as.naming.InitialContextFactory cannot be cast to javax.naming.spi.InitialContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657) [rt.jar:1.6.0_26]
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) [rt.jar:1.6.0_26]
at javax.naming.InitialContext.init(InitialContext.java:223) [rt.jar:1.6.0_26]
at javax.naming.InitialContext.<init>(InitialContext.java:197) [rt.jar:1.6.0_26]
at de.haba.context.ContextUtil.getDataSource(ContextUtil.java:30) [contextutil.jar:]
{noformat}
No, the system property alternative i haven't tried yet.
-
3. Re: how to lookup datasource within module
sfcoy May 21, 2013 2:10 AM (in response to haribo)1 of 1 people found this helpfulChances are you need more than javax.naming as a dependency. Try javax.api instead.
-
4. Re: how to lookup datasource within module
haribo May 21, 2013 2:23 AM (in response to sfcoy)Thanks a lot, it works now.
But this make me something like this:
I thought to depend only on what you obviously need, but now i can do it like in the past (4.2.3): depend on everything to have no problems at all.
Those are the days i'am asking myself giving up.
-
5. Re: how to lookup datasource within module
sfcoy May 21, 2013 2:30 AM (in response to haribo)I know what you mean, but sometimes you need to be pragmatic.
You could narrow it down with trial and error, but javax.naming is provided by the JDK and it's internal workings may change resulting in other deps that happen to be in the same JDK.
-
6. Re: how to lookup datasource within module
nickarls May 21, 2013 2:41 AM (in response to haribo)But this make me something like this:
I thought to depend only on what you obviously need, but now i can do it like in the past (4.2.3): depend on everything to have no problems at all.
Those are the days i'am asking myself giving up.
You must be new in the field if that makes you want to give up
If you really want to be depressed, I recommend you start working with JSF and any of the major *Faces component libraries with client browser requirements of IE6.
-
7. Re: how to lookup datasource within module
haribo May 21, 2013 3:13 AM (in response to nickarls)Yeah, i'm new in the field of 7.x, giving up in the manner of migrating 4.2.3 to 7.1.1. I think i should do everything new instead of migrating
IE6 is definitely a challenge (and this also without JSF), you have my deepest sympathy
-
8. Re: how to lookup datasource within module
masummymesingh Jan 4, 2014 7:31 AM (in response to haribo)Remote Datasource connection with Jboss 7
=============================================
// <datasource jndi-name="java:jboss/datasources/SampleDS" pool-name="SampleDS" enabled="true" use-java-context="true"> public static void main(String[] args) throws SQLException {
DataSource cname = null; Context context = null; try { Properties properties = new Properties(); // properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory"); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); context = new InitialContext(properties); cname = (DataSource) context.lookup("java:jboss/datasources/SampleDS"); Connection connection = cname.getConnection(); System.out.println("connection-----M : "+ connection.isClosed()); } catch (NamingException e) { e.printStackTrace(); } }
============================================
Error :
javax.naming.NameNotFoundException: jboss/datasources/SampleDS --
service jboss.naming.context.java.jboss.exported.jboss.datasources.SampleDS
-
9. Re: how to lookup datasource within module
wdfink Jan 4, 2014 10:33 AM (in response to masummymesingh)Remote access to datasources are not supported in AS7 and further, as it is a bad idea.
-
10. Re: how to lookup datasource within module
masummymesingh Jan 4, 2014 12:27 PM (in response to wdfink)can you provide alternative solution for this purpose ?
is EJB as an intermediary in accessing the data source ??
-
11. Re: how to lookup datasource within module
wdfink Jan 4, 2014 4:41 PM (in response to masummymesingh)As you read this in the Red Hat knowledgebase you can be sure that this will be the recommended solution as it will be continuous reviewd by supporter an engineers
So yes use EJB's to wrap the database access.
-
12. Re: how to lookup datasource within module
masummymesingh Jan 5, 2014 1:05 AM (in response to wdfink)Hi, i am trying to solve this problem using ejb's wrap to db connection but I have got this problem ..
------------------------------------------------------------------------------------
@Remote
public interface ProductServiceImplRemote {
Connection getConnection();
.......
public static void main(String[] args) throws SQLException {
DataSource cname = null;
Context context = null;
ProductServiceImplRemote bean = null;
try {
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
context = new InitialContext(properties);
bean = (ProductServiceImplRemote) context.lookup("ejb:/ejbdemo1//ProductServiceImpl!com.bus.ProductServiceImplRemote");
System.out.println(bean.getConnection());
Error :
bean.getConnection ..............Null
jboss ejb side error is :
org.jboss.marshalling.river.RiverMarshaller.doWriteObject(R
(EJB default - 2) Could not write method invocation result for method
public abstract java.sql.Connection com.bus.ProductServiceImplRemote.getConnection() on bean named
in object org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@174632b
-
13. Re: how to lookup datasource within module
wdfink Jan 6, 2014 2:26 PM (in response to masummymesingh)Not that way.
You can not pass the connection to a remote client neither by JNDI lookup nor by returning the connection.
What is meant by wrapping the connection is to have all the code which need the connection reside inside the EJB.
pass the parameter to the EJB, inside the EJB open the connection, manipulate the data and close the connection.
You may return data or not.