Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you use java stored procedures with an Oracle database ... what are the drawbacks? [closed]

PL/SQL is not my native tongue. Oracle supports writing stored procedures in Java. What are the advantages of doing this over writing the stored procedures in PL/SQL

like image 203
minty Avatar asked Sep 16 '08 07:09

minty


People also ask

Why you should not use stored procedures?

Stored procedures promote bad development practices, in particular they require you to violate DRY (Don't Repeat Yourself), since you have to type out the list of fields in your database table half a dozen times or more at least. This is a massive pain if you need to add a single column to your database table.

What is Java stored procedure in Oracle?

A Java Stored Procedure is a procedure coded in Java (as opposed to PL/SQL) and stored in the Oracle database. Java Stored procedures are executed by the database JVM in database memory space. Java Stored Procedures can be developed in JDBC or SQLJ. Interfacing between PL/SQL and Java are extremely easy.


2 Answers

In the Oracle world the general order of development should be:

Wherever possible do it purely with SQL. If you need more than SQL do it with PL/SQL. If you need something that PL/SQL can't do, then use Java. If all else fails use C. If you can't do it with C, back slowly away from the problem....

PL/SQL stored procedures are an excellent way of moving your business logic to a layer that will be accessible by any integration technology. Business Logic in a Package (don't write stand alone Functions and Procedures - they'll grow over time in an unmanageable way) can be executed by Java, C#, PL/SQL, ODBC and so on.

PL/SQL is the fastest way to throw around huge chunks of data outside of pure SQL. The "Bulk Binding" features means it works very well with the SQL engine.

Java stored procedures are best for creating functionality that interacts with network or operating system. Examples would be, sending emails, FTP'ing data, outputting to text files and zipping it up, executing host command lines in general.

I've never had to code up any C when working with Oracle, but presumably it could be used for integrating with legacy apps.

like image 53
Rob Paterson Avatar answered Oct 09 '22 05:10

Rob Paterson


Only when you can't do it in PL/SQL ( or PL/SQL proves to be too slow, which would be pretty rare I believe ).

As a case study... We had a single java stored procedure running in production ( Oracle 9i ), it was originally written in java because at the time we thought java was cool, Something I've long since changed my mind about. Anyway. One day, the DB crashes, after it reboots the java SP doesn't work. After much back and forth with oracle support, they don't really know what the problem is and the only suggestions they have involve much downtime. Something which wasn't an option. 30 minutes later I had rewritten the java SP in PL/SQL.

It now, runs faster, is oracle "native" , shares the same deployment process as other objects and is easier to debug.

PL/SQL is a very capable language. If you are writing Stored Procedures, please take the time to learn it rather than just doing things in java because thats what you know.

like image 11
Matthew Watson Avatar answered Oct 09 '22 06:10

Matthew Watson