Skip to Main Content
  • Questions
  • JAVA in Oracle -- when and how long does the JVM get started /stay around

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Reji.

Asked: June 27, 2000 - 3:18 pm UTC

Last updated: December 17, 2004 - 2:43 pm UTC

Version: 8.1.6

Viewed 1000+ times

You Asked

Tom:

I have a trigger in one of the table, when I insert a record
this trigger get fired and that will execute a stored
procedure which in turn invoke a method from a java class - This method open a socket and sends a msg to an external process.

When the stored procedure invoke the method from the java class, does oracle creates a new VM? if it is what is the life span of that VM? If I have so many insert into this table, does
that mean I will have so many of these VM up and running at the same time? What is the overhead of doing this way? or Is there any other way I can accomplish the same thing?

In my case, after I finish sending the msg to the external
process, do I have to close the socket explicitly?


TIA
Reji




and Tom said...

The VM is on a session by session basis.

Once you have started your mini-jvm, it'll be there until you log out.

The first java call has some measurable overhead. Subsequent calls are instantaneous.


Ultimately you should close the socket yourself -- yes. Do you have to do it right away -- no, you can have a "cleanup" call later on to close the socket instead of opening/closing it each and every time.

If you just need sockets, you can do it in plsql with UTL_TCP in 8.1.6.

Rating

  (9 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Another one

Kumar, May 03, 2002 - 8:48 am UTC

so if 100 user connected to oracle ,100 jvm instance will be there ,is that right?

Tom Kyte
May 03, 2002 - 11:18 am UTC

there is one real JVM, each session will get their own VJVM (a virtual java virtual machine). The all share the same "big" jvm. They each have their own "session" however (I cannot see your globals/static data and such)

How many no VJVM?

Kumar, May 03, 2002 - 5:13 pm UTC

How will i know how many no VJVM is running?

Tom Kyte
May 03, 2002 - 7:31 pm UTC

I don't know what you mean.

JVM

Vijays, May 04, 2002 - 3:20 am UTC

How do we know which version of JVM running ?

Tom Kyte
May 04, 2002 - 1:14 pm UTC

create or replace and compile java source named "props" as
public class props {
  public static void show_props() {
    System.getProperties().list(System.out);
  }
}
/
show errors
create or replace procedure java_props
as language java name 'props.show_props()';
/
show errors
exec dbms_java.grant_permission( user, 'java.util.PropertyPermission','*','read,write' );
set serveroutput on size 10000
exec dbms_java.set_output(10000)
exec java_props


will do it:

ops$tkyte@ORA817DEV.US.ORACLE.COM> exec java_props
-- listing properties --
java.specification.name=Java Platform API Specification<b>
java.version=1.2.1</b>
oracle.aurora.mts.INIT=oracle.aurora.mts.rdbms.INIT
user.timezone=EST 

JVM Performance

phil, August 23, 2004 - 2:59 am UTC

Hi Tom,

We have come to a point where we are seeing the Oracle JVM running java code more than 10 times slower than running the java code outside of Oracle. I have read a number of threads on Metalink where other users see similar issues. There seems to be no response from Oracle on these threads. Unfortunalty I cannot find where I can optimise this. We have altered database parameters in many ways to see if we can get changes, but to no avail.

Could you comment on your own experience with this. Is the Oracle JVM really inefficient ?

many thanks




Tom Kyte
August 23, 2004 - 7:32 am UTC

i cannot respond as I would never actually run any truly intense java inside my database.

if I could not do it in sql, i would do it in plsql. and if i could not do it in plsql -- I would use the smallest, tiniest piece of java possible called from plsql.


but given you give me nothing to actually *look at* here, i don't have that much to say.

continuation of above

phil, August 23, 2004 - 4:08 am UTC

sorry ..

the version of Oracle we are using is 9i r2. Also the java code we are testing with receives an object from a plsql stored procedure, this object is an array of about 5000 rows of data. The java returns a clob which is of XML. We have tried similar tests , whereby we just call the java passing the object and returning a string. The performance was the same.

many thanks


Tom Kyte
August 23, 2004 - 8:10 am UTC

no benchmark, no comment. something small, concise, yet 100% complete -- that we could all give a go with.

something to go on

phil, August 24, 2004 - 3:02 am UTC

appologies...

something simple ...

create or replace and compile
java source named "Mysp"
as
public class Mysp extends Object {
public static double test() {
double d = 0.0;
long p = System.currentTimeMillis();
for (int i=0; i<10000000; i++) {
d= d+ (i+0.1)*2;
}
long p2 = System.currentTimeMillis();
return d;
}
}
/

create or replace function test return number as
language java name 'Mysp.test() return double';

user@db> declare
2 n long;
3 begin
4 dbms_output.put_line(to_char(sysdate, 'dd/mm/yy hh:mi:ss'));
5 select test() into n from dual;
6 dbms_output.put_line(to_char(sysdate, 'dd/mm/yy hh:mi:ss'));
7 end;
8 /
24/08/04 06:15:50
24/08/04 06:15:58

PL/SQL procedure successfully completed.

as pure java .... 78 miliseconds


import java.util.Date;

public class My
{
public static void main(String[] args)
{
Date start = new Date();
double d = 0.0;
long p = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++)
{
d = d + (i + 0.1) * 2;
}
long p2 = System.currentTimeMillis();
System.out.println((p2 - p) / 1000.0);
System.out.println(d);
Date end = new Date();
System.out.println( "diff " +(end.getTime() - start.getTime()) );
}
}

==============
0.078
9.999999201065402E13
diff 78


Tom Kyte
August 24, 2004 - 7:51 am UTC

guess you could try native compilation (or C)

How to find JVM (Java) Version in Oracle

Prasad Gunaratne, September 21, 2004 - 7:45 pm UTC


what do u mean by procedures and y do we go for it ?

Balaji, December 17, 2004 - 9:04 am UTC

where can i get a detailed description for procedures ?

Tom Kyte
December 17, 2004 - 2:43 pm UTC

?

guess I would start with the plsql documentation?

java oracle

Amit Kumar, January 18, 2007 - 4:59 am UTC

how to pass sysdate from java funtion to the oracle table

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library