Thread: Can I call an EJB from the database?


Permlink Replies: 15 - Pages: 2 [ 1 2 | Next ] - Last Post: Aug 21, 2009 4:59 AM Last Post By: MeenakshiShri
sangs

Posts: 5
Registered: 08/30/06
Can I call an EJB from the database?
Posted: Aug 30, 2006 9:23 AM
Click to report abuse...   Click to reply to this thread Reply
Hi,

I've just spent the last few days attempting to call an EJB from a stored procedure in a 10g database (and still haven't succeeded).
Now having spent hours searching the web (and this forum) for any successful examples (so far I have found none) I thought I'd add a message to see if there is anybody out there who has done this successfully. If so can you please provide me some details on how to do this?

Alternatively, I'd appreciate any input on how I can overcome my current hurdle in attempting to do this myself.

Here's how far I've got in my attempt:

I have a simple stateless session bean deployed to both Oracle 10gAS (a standalone OC4J container in 10.1.3) and to Weblogic 9. I have a client java class that calls a method on this bean and displays some ouput if successful. This all works from a standalone JVM (version 1.4) on both Weblogic and 10gAS.

I then tried to load the required libraries for Weblogic onto the database (namely the Weblogic.jar file). This resulted in the addition of some 32,000 odd objects of which 30,000 odd could not be resolved. I figured a lot of these may be down to JVM version issues (I believe that Weblogic 9 uses the 1.5 JRE) so I decided to put the Weblogic test on the back burner for now and just use the Oracle app server.

The tests with the Oracle app server looked promising. I only needed a handful of libraries to be loaded onto the database and all the relevant classes required for my test client had resolved (including the initial context factory required to do the jndi lookup of the bean). After granting all the permissions I believed I needed I'd got to the point where I could do a jndi lookup of the EJB; but when I tried to create an instance of the beans remote interface I got the following error:

java.security.AccessControlException: the Permission (java.lang.RuntimePermission getClassLoader) has not been granted to ProtectionDomain (file:generated/by/proxy <no certificates>)
com.evermind.net.DynamicClassLoader@612d9d34
<no principals>
java.security.Permissions@eb0f3c1a (
(java.util.PropertyPermission java.version read)
(java.util.PropertyPermission java.vm.name read)
(java.util.PropertyPermission java.vm.vendor read)
(java.util.PropertyPermission os.name read)
(java.util.PropertyPermission java.vendor.url read)
(java.util.PropertyPermission java.vm.specification.vendor read)
(java.util.PropertyPermission java.specification.vendor read)
(java.util.PropertyPermission os.version read)
(java.util.PropertyPermission java.specification.name read)
(java.util.PropertyPermission java.class.version read)
(java.util.PropertyPermission file.separator read)
(java.util.PropertyPermission java.vm.version read)
(java.util.PropertyPermission os.arch read)
(java.util.PropertyPermission java.vm.specification.name read)
(java.util.PropertyPermission java.vm.specification.version read)
(java.util.PropertyPermission java.specification.version read)
(java.util.PropertyPermission java.vendor read)
(java.util.PropertyPermission path.separator read)
(java.util.PropertyPermission line.separator read)
(java.net.SocketPermission localhost:1024- listen,resolve)
)
.
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:280)
at java.security.AccessController.checkPermission(AccessController.java:429)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:528)
at oracle.aurora.rdbms.SecurityManagerImpl.checkPermission(SecurityManagerImpl.java:192)
at java.lang.Thread.getContextClassLoader(Thread.java:1203)
at com.evermind.server.rmi.RMICall.<init>(RMICall.java:36)
at com.evermind.server.rmi.RmiCallQueue.createCall(RmiCallQueue.java:33)
at com.evermind.server.rmi.RMIClientConnection.createQueuedCall(RMIClientConnection.java:592)
at com.evermind.server.rmi.RMIClientConnection.writeRequest(RMIClientConnection.java:581)
at com.evermind.server.rmi.RMIClientConnection.sendMethodInvocationRequest(RMIClientConnection.java:426)
at com.evermind.server.rmi.RMIClientConnection.invokeMethod(RMIClientConnection.java:415)
at com.evermind.server.rmi.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:63)
at com.evermind.server.rmi.RecoverableRemoteInvocationHandler.invoke(RecoverableRemoteInvocationHandler.java:28)
at __Proxy0.create(Unknown Source)
at com.axiomsystems.test.client.TestDBClient.test(TestDBClient.java:76)

Has anybody come across this before? If so can they help?

If not has anybody out there actually managed to call an EJB method (without going via an RMI server/Servlet/or any other proxy).

Any help in this matter would be greatfully appreciated.

Cheers,

Anand.
Avi Abrami

Posts: 4,090
Registered: 07/02/00
Re: Can I call an EJB from the database?
Posted: Aug 30, 2006 12:29 PM   in response to: sangs in response to: sangs
Click to report abuse...   Click to reply to this thread Reply
sangs,
I don't have the book, but I believe that [url=http://www.amazon.com/gp/product/1555583296/]Oracle Database Programming Using Java and Web Services[/url] by Kuassi Mensah has an example of EJB-callout.

Good Luck,
Avi.
sangs

Posts: 5
Registered: 08/30/06
Re: Can I call an EJB from the database?
Posted: Aug 31, 2006 2:24 AM   in response to: Avi Abrami in response to: Avi Abrami
Click to report abuse...   Click to reply to this thread Reply
Thanks for the link Avi, looks like a useful book to have. I'll update this link if I do manage to get this working.
sangs

Posts: 5
Registered: 08/30/06
Re: Can I call an EJB from the database?
Posted: Sep 20, 2006 2:19 AM   in response to: sangs in response to: sangs
Click to report abuse...   Click to reply to this thread Reply
This message is for anybody out there attempting to do what I was doing.

I have now managed to successfully call an EJB deployed on an OC4J container (10.1.3) via a stored procedure in the database (Oracle 10.2.0.1).

Once you have your application deployed to the app server and have a client that successfully calls the EJB in a standalone JVM you can try and make the callout from the database. (Note that I used "com.evermind.server.rmi.RMIInitialContextFactory" for the context factory.)

You will need to load the following libraries onto the database schema (using loadjava):

OC4J_HOME\webservices\lib\saaj-api.jar
OC4J_HOME\webservices\lib\jaxrpc-api.jar
OC4J_HOME\j2ee\home\lib\ejb.jar
OC4J_HOME\opmn\lib\optic.jar
OC4J_HOME\j2ee\home\lib\jmxri.jar
OC4J_HOME\j2ee\home\lib\javax77.jar
OC4J_HOME\jlib\javax-ssl-1_1.jar
OC4J_HOME\j2ee\home\lib\oc4j_orb.jar
OC4J_HOME\j2ee\home\iiop.jar
OC4J_HOME\j2ee\home\lib\orbbase.jar
OC4J_HOME\j2ee\home\lib\iiop_support.jar
OC4J_HOME\j2ee\home\oc4jclient.jar

They should all resolve once loaded (use utl_rcomp to ensure they are all valid).

Then load your relevant EJB client and create a stub PL/SQL procedure. You will need to grant relevant java security permissions to the user who owns the procedure (e.g. the following are some of the permissions I had to grant:
exec dbms_java.grant_permission( 'TESTUSER','SYS:java.util.PropertyPermission', 'java.net.preferIPv4Stack', 'write' );
exec dbms_java.grant_permission( 'TESTUSER','SYS:java.util.logging.LoggingPermission', 'control', '' );
exec dbms_java.grant_permission( 'TESTUSER', 'SYS:java.net.SocketPermission','*', 'connect,resolve' );
exec dbms_java.grant_permission( 'TESTUSER', 'SYS:java.net.SocketPermission','127.0.0.1:23791', 'connect,resolve' );
exec dbms_java.grant_permission( 'TESTUSER', 'SYS:java.io.SerializablePermission','enableSubstitution', '' );
exec dbms_java.grant_permission( 'TESTUSER', 'SYS:java.lang.RuntimePermission','getClassLoader', '' );
exec dbms_java.grant_permission( 'TESTUSER', 'SYS:java.lang.RuntimePermission','setContextClassLoader', '' );
exec dbms_java.grant_permission( 'TESTUSER', 'SYS:java.lang.RuntimePermission','createClassLoader', '' );
).

The final step is to update the security policy in the oracle JVM. This is done by creating/updating the java.policy file in the Oracle JVM location (ORACLE_HOME/javavm/lib/security) and setting the relevant permissions (e.g. in my test case I granted all permissions using the following:

GRANT {
permission java.security.AllPermission;
};

).

I've not seen the example in the book mentioned in this post as I'm still waiting for Amazon to ship it! For some reason this book is taking some time to get to the UK. This problem was solved with the help from Oracle support who pointed me to the fact that java policy file that resides in the Oracle JVM folder also needed updating.

Hope this helps.
plambis

Posts: 1
Registered: 11/16/06
Re: Can I call an EJB from the database?
Posted: Nov 16, 2006 6:49 AM   in response to: sangs in response to: sangs
Click to report abuse...   Click to reply to this thread Reply
sangs, 10x for your last post. It was very useful and helpful.

I use your solution to call EJB2 which is on remote jboss server from Java Oracle Function.

It works great.

Before I grant your permissions for my oracle schema. The my function throw me Exception on context.lookup().
matelot

Posts: 35
Registered: 07/31/06
Re: Can I call an EJB from the database?
Posted: Jan 22, 2007 7:30 PM   in response to: sangs in response to: sangs
Click to report abuse...   Click to reply to this thread Reply
This message is for anybody out there attempting to

> do what I was doing.

>

> I have now managed to successfully call an EJB

> deployed on an OC4J container (10.1.3) via a stored

> procedure in the database (Oracle 10.2.0.1).


Oh bummer <weep>, I (on 10gR2) seem to miss 6 of those files listed


[oracle/product/10.2/oc4j]$ ls webservices/lib/saaj-api.jar \

> webservices/lib/jaxrpc-api.jar \

> opmn/lib/optic.jar \

> j2ee/home/lib/oc4j_orb.jar \

> j2ee/home/lib/orbbase.jar \

> j2ee/home/lib/iiop_support.jar


webservices/lib/saaj-api.jar: No such file or directory

webservices/lib/jaxrpc-api.jar: No such file or directory

opmn/lib/optic.jar: No such file or directory

j2ee/home/lib/oc4j_orb.jar: No such file or directory

j2ee/home/lib/orbbase.jar: No such file or directory

j2ee/home/lib/iiop_support.jar: No such file or directory



Now what ?

Help anyone ?

thanks
kmensah

Posts: 676
Registered: 01/10/01
Re: Can I call an EJB from the database?
Posted: Jan 24, 2007 3:39 PM   in response to: sangs in response to: sangs
Click to report abuse...   Click to reply to this thread Reply
Hi,

You can grab these jars from an OC4J install (here is an excerpt from EJB Callout in section 4.2.2 chapter 4 of my book)

a) Download a recent edition of OC4J stand-alone (for development
and test purposes only).
http://www.oracle.com/technology/software/products/ias/index.html

b. Install OC4J stand-alone by extracting the ZIP file in a directory
of your choice (e.g., C:\OC4J) and follow the Basic Installation
instructions in the Readme file.

The following step is for starting OC4J with RMI/IIOP listener

c. By default, OC4J uses RMI over ORMI (an Oracle/Orion proprietary
but optimized protocol as opposed to standard IIOP);
GeneratedIIOP startup option enables RMI/IIOP instead. Start
OC4J as follows (from <install dir>/j2ee/home, referred to as
$J2EE_HOME):
java -DGenerateIIOP=true -jar oc4j.jar

Kuassi, http://db360.blogspot.com
mishaerror

Posts: 33
Registered: 05/16/06
Re: Can I call an EJB from the database?
Posted: Feb 14, 2007 7:56 AM   in response to: kmensah in response to: kmensah
Click to report abuse...   Click to reply to this thread Reply
This topic has been very helpfull to me.
I have managed to load java rmi client into database, made wrapper function etc.
However, when I ran it, I got the exception

Cannot instantiate class: oracle.j2ee.rmi.RMIInitialContextFactory

My classes are compiled in jdev 10.1.3.1, so i guess that means jdk 1.5

The database I am trying to run my RMI client has jdk 1.4

Could this be the problem?
kmensah

Posts: 676
Registered: 01/10/01
Re: Can I call an EJB from the database?
Posted: Feb 14, 2007 9:49 AM   in response to: mishaerror in response to: mishaerror
Click to report abuse...   Click to reply to this thread Reply
Hi,

Loading JDK 5 code in a JDK4 VM might be just part of the problem; i would suggest to look more in the JNDI context factory directtion. If you look at the working example of EJB callout in chapter 4 of my book, i tweak the JNDI propertie file to use a standard "com.sun.jndi.cosnaming.CNCtxFActory" to use omg.omb.CORBA lookup.

Good luck,
Kuassi http://db360.blogspot.com
mishaerror

Posts: 33
Registered: 05/16/06
Re: Can I call an EJB from the database?
Posted: Mar 6, 2007 8:59 AM   in response to: kmensah in response to: kmensah
Click to report abuse...   Click to reply to this thread Reply
The fun continues.

I have experimented more with this issue and found some very weird results.

First, to avoid problems with different versions of Java, i used java 1.4 for my application, so i used, for the purpose of experiment, EJB 2.1 stateless bean.

So, my client looks up thqat bean on a server. I loaded and resolved everithing and its all valid.

When i perform lookup of the bean, I get NoClassDefFound exception.

What could be the problem? All classes I loaded are valid. Withouth the lookup part of the code client works fine (it even gets initialContext without any exceptions), as soon as I lookup the bean I get the NoClassDefFound.
Avi Abrami

Posts: 4,090
Registered: 07/02/00
Re: Can I call an EJB from the database?
Posted: Mar 6, 2007 10:06 PM   in response to: mishaerror in response to: mishaerror
Click to report abuse...   Click to reply to this thread Reply
user510152,
It may help if you post the entire error message and stack trace you are getting as well as the part of your code where you get the "Context" object and perform the lookup.

Good Luck,
Avi.
mishaerror

Posts: 33
Registered: 05/16/06
Re: Can I call an EJB from the database?
Posted: Mar 7, 2007 1:10 AM   in response to: Avi Abrami in response to: Avi Abrami
Click to report abuse...   Click to reply to this thread Reply
Sure, here's the code. It's what JDev generates automatically but I have seperated some things into several diffrent lines so I could experiment easier.

...
final Context context = getInitialContext();

Object o=context.lookup( "SessionEJB4" );

Object l= PortableRemoteObject.narrow( o, SessionEJB4Home.class );
final SessionEJB4Home sessionEJB4Home = (SessionEJB4Home)l;

SessionEJB4 sessionEJB4 = sessionEJB4Home.create();

String ret=( sessionEJB4.hello( ) );
...

As I mentioned, for this I have used EJB2.1 bean, because I had same problems with EJB3.0, so I tried earlier versions of Java to make sure that is not the problem.

hello() method just returns string "hello".

Of course, this works in standalone client that is not loaded into database.

Now, I loaded all the jars mentioned in this thread, using loadjava options -v -resolve, and I loaded client using options -v -resolve -genmissing . Select query on user objects in database shows that client class and relevant classes are all valid.

Then I created stub and ran it. First it stopped throwing exception ORA-29532: Java call terminated by uncought Java exception: Java.lang.NoClassDefFoundError.

Method I try to run returns String value which is hello message if it works fine, else it prints the part of the stack trace. In this case, after p[revious error, When I try to run it again, i get return from the method :

Lookup error: java.lang.NoClassDefFoundError; nested exception is:
java.lang.NoClassDefFoundError
com.evermind.server.rmi.RMICall.throwRecordedException(RMICall.java:89)
com.evermind.server.rmi.RMIClientConnection.waitForJndiResponse(RMIClientConnection.java:371)
com.evermind.server.rmi.RMIClientConnection.lookup(RMIClientConnection.java:179)
com.evermind.server.rmi.RMIClient.lookup(RMIClient.java:283)
com.evermind.server.rmi.RMIClientContext.lookup(RMIClientContext.java:51)
javax.naming.InitialContext.lookup(InitialContext.java:347)
simpleejbproject.EJB4Client.hello(EJB4Client.java:35)

Of course, regular version of the client I run frokm command line works fine.

So I'm kind of a stuck here. I followed all directions, tried it on oracle 9 and oracle 10 DB, the problems remain the same. Am I missing something here?

Thanks

Just to add, getInitialContext()

private static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
// Oracle Application Server 10g connection details
env.put( Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory" );
env.put( Context.SECURITY_PRINCIPAL, "oc4jadmin" );
env.put( Context.SECURITY_CREDENTIALS, "manager1" );
env.put(Context.PROVIDER_URL, "opmn:ormi://mserv:4005:home/TestEjb4");
return new InitialContext( env );
}

It goes withouth any exceptions.
Message was edited by:
user510152

mishaerror

Posts: 33
Registered: 05/16/06
Re: Can I call an EJB from the database?
Posted: Mar 8, 2007 5:45 AM   in response to: mishaerror in response to: mishaerror
Click to report abuse...   Click to reply to this thread Reply
_

Message was edited by:
user510152
kmensah

Posts: 676
Registered: 01/10/01
Re: Can I call an EJB from the database?
Posted: Mar 10, 2007 11:54 PM   in response to: mishaerror in response to: mishaerror
Click to report abuse...   Click to reply to this thread Reply
Hi,

From the stack trace, it looks like the problem is related to the JNDI lookup service you are usin.
In the example i gave in chapter 4 of my book, i used the org.omb.CORBA lookup services to invoke an EJB deployed in OC4J, as follows (content of the jndi.properties):

java.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
java.naming.provider.url=corbaname::localhost:5555#helloworld
java.naming.security.principal=admin
java.naming.security.credentials=welcome


You can download the EJB code sample from http://books.elsevier.com/companions/9781555583293/casestudies/05~chapter04.zip

Kuassi http://db360.blogspot.com
skinsella

Posts: 3
Registered: 04/24/07
Re: Can I call an EJB from the database?
Posted: Apr 24, 2007 4:59 AM   in response to: sangs in response to: sangs
Click to report abuse...   Click to reply to this thread Reply
I have a similar question, can you help:

http://forums.oracle.com/forums/thread.jspa?threadID=500108
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums