Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2005

Tip: Looking for answers? Try searching our database.

Why did COMMIT solved my Java->PL/SQL problem?

Thread view: 
ohaya - 21 Jun 2005 07:04 GMT
Hi,

I originally posted the msg below on the Oracle.server NG, and since
then, I've found that I was able to get this working by simply adding a
COMMIT at the end of my PL/SQL procedure (see msg below for code).  Now
that it's kind of working, I am trying to understand "why?".

Can anyone explain why the PL/SQL appeared to not "return" to Java when
the COMMIT was absent in the PL/SQL procedure?  

When writing PL/SQL procedures that are intended to work with Java,
should I always add a COMMIT at the end of the PL/SQL procedures?

Thanks,
Jim

======= Previous msg posted in comp.databases.oracle.server
================
Hi,

I'm trying to call a simple Oracle stored procedure (which I created and
which uses the DBMS_OBFUSCATION_TOOLKIT) from my Java code, but am
having problems.

Here's my stored procedure:

CREATE or REPLACE PROCEDURE encrypt_password (key_string varchar2, uname
varchar2, password varchar2) IS

input_string    VARCHAR2(16) := to_char(password);
encrypted_string VARCHAR2(2048);

BEGIN
       dbms_obfuscation_toolkit.DESEncrypt(
                               input_string => input_string,
                               key_string => key_string,
                               encrypted_string => encrypted_string);
       UPDATE XXDB set secure_pwd = encrypted_string WHERE username =
uname;

<======= ADDING A "COMMIT;" here seemed to eliminate the problem
==========>

END;

The Java code looks like:

public static void doEncryption(String enKey, String uName, String pwd)
throws SQLException {

               Connection conn = null;
               CallableStatement proc = null;

      try
      {
       // Load the Oracle JDBC driver
       DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());

       System.out.println("In Provider/initialize: About to open
connection to Oracle DB...");

           // Open the connection
       conn = DriverManager.getConnection
("jdbc20:oracle:thin:@jimnew:1521:XX","XX_OWNER", "XXXXXX");
       System.out.println("In Provider/initialize:  Finished opening
connection to Oracle DB...");
       
         proc = conn.prepareCall("{ call encrypt_password(?, ?, ?) }");
         proc.setString(1, enKey);
         proc.setString(2, uName);
         proc.setString(3, pwd);
         System.out.println("In Provider/initialize:  Finished setting
parameters - about to execute procedure");
         proc.execute();
         System.out.println("In Provider/initialize:  Just returned
from proc.execute()");
      }
      finally
      {
         try
         {
            proc.close();
         }
         catch (SQLException e) {}
         conn.close();
      }
   }

The output I'm getting is as follows:

.
.
In Provider/initialize: About to open connection to Oracle DB...
In Provider/initialize:  Finished opening connection to Oracle DB...
In Provider/initialize:  Finished setting parameters - about to execute
procedure

There is no other output after that last line, i.e., it seems to have
done the "proc.execute()" but never returned.  I don't see the last
println ("Just returned from proc.execute()"), and I don't get any
errors!

Can anyone tell me what the problem might be?  Or, how I might try to
diagnose why it's not returning from the "proc.execute()"?

Thanks,
Jim
Lee Fesperman - 21 Jun 2005 09:51 GMT
> Hi,
>
[quoted text clipped - 11 lines]
> Thanks,
> Jim

Try putting a println in your finally block. If you see that, try one in
the catch block inside the finally. You didn't show the code outside the
method, so we have to assume you are printing any SQLException thrown by
the method. Are you?

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

ohaya - 21 Jun 2005 11:31 GMT
> > Hi,
> >
[quoted text clipped - 16 lines]
> method, so we have to assume you are printing any SQLException thrown by
> the method. Are you?

Lee,

In answer to your last question, yes, I have a println when the method
is called:

       try {
           doEncryption("xxxxxxxx", "test1", "xxxxxxxxxxxxxxxx");
           } catch (SQLException e) {
               System.out.println("Error when tried doEncryption:" + e);
               }

Here's the strange thing.  I added a println to the finally, as you
suggested, and I deleted the "COMMIT" in the procedure to re-run the
Java program this morning, and this time, it STILL worked!!

In other words, now this is all working without the COMMIT in the
procedure, whereas last night, the Java program appeared to not return
from the "proc.execute()".

Now, I am really puzzled!!

Is it possible that calling the procedure with the COMMIT in it a couple
of times cleared up the problem, and now the COMMIT is no longer needed
in the procedure?

Jim
Lee Fesperman - 22 Jun 2005 00:42 GMT
> > > Hi,
> > >
[quoted text clipped - 27 lines]
>                 System.out.println("Error when tried doEncryption:" + e);
>                 }

No, I meant the catch inside the finally block. However, ...

> Here's the strange thing.  I added a println to the finally, as you
> suggested, and I deleted the "COMMIT" in the procedure to re-run the
[quoted text clipped - 9 lines]
> of times cleared up the problem, and now the COMMIT is no longer needed
> in the procedure?

It's good that it is now working, but I don't see why committing a few times would clear
up the problem. It is possible you have an old copy of the procedure cataloged?

I still think you need to add a println to the catch in the finally block.

However, your description is still vague. You say that proc.execute() doesn't return.
What does that mean? Does the program never terminate so that you have to cancel it?

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

ohaya - 22 Jun 2005 01:28 GMT
Lee,

Comments interspersed below....

Jim

> > > Try putting a println in your finally block. If you see that, try one in
> > > the catch block inside the finally. You didn't show the code outside the
[quoted text clipped - 13 lines]
>
> No, I meant the catch inside the finally block. However, ...

Ok.  I did that:

           finally {
               System.out.println("In finally, before try-close");
               try {
                   proc.close();
               }
               catch (SQLException e) {
                   System.out.println("Error in doEncryption: " + e);
               }
               catch (Exception f) {
                   System.out.println("Error in doEncryption/Exception:" + f);
               }

           System.out.println("About to do conn.close()");
           conn.close();
           System.out.println("Connection to Oracle DB closed");
           } // end finally


> > Here's the strange thing.  I added a println to the finally, as you
> > suggested, and I deleted the "COMMIT" in the procedure to re-run the
[quoted text clipped - 17 lines]
> However, your description is still vague. You say that proc.execute() doesn't return.
> What does that mean? Does the program never terminate so that you have to cancel it?

I don't understand why adding the COMMIT seemed to have gotten it going,
but it's actually a bit stranger than that.  

Once it was "working", I was doing more testing, and wanted to go back
and verify that including the COMMIT was what made it work.  So, I
deleted the COMMIT from the procedure, but after that deletion, my
program still kept working.

Then, sometime later, it started not working again.

I then put the COMMIT back into the procedure, and it started working
again.

For now, I'm just leaving the COMMIT in the procedure...

You mentioned that my description was vague, and that I said that
proc.execute doesn't return, and were asking what I meant by this.

What I meant is that I have the following in my code:

System.out.println("In Provider/initialize:  Finished setting parameters
- about to execute procedure");
proc.execute();
System.out.println("In Provider/initialize:  Just returned from
proc.execute()");

where I have the "proc.execute();" surrounded by println's.  When the
program is working properly, I can see the output from the first and
second println.  When the program is "not" working, I can see the output
from the first println, only but then no output from the program after
that.

My conclusion was that the proc.execute() within my Java program was
being executed, but then control never returned to my Java program.

The program doesn't seem to be terminating...

BTW, I hadn't mentioned this earlier, because it hadn't occurred that it
could be relevant, but I'm running my program under the Eclipse IDE.  Is
it possible that the proc.execute() not returning might be because it's
running under Eclipse?

Jim


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.