Exclusive access to a stored procedure

Hi all,
I've the following question:
Is it possible to set an exclusive access for sessions to a procedure in the DB?
So if session A enters the procedure and runs for example a loop or something like that and then session B comes and needs to enter the same procedure, it has to wait till session A finished and left the procedure.
If it is possible, how?
Thanks in advance.

> If it is possible, how?
It is called serialisation - forcing a resource (a procedure in your case) to service only a single caller at a time.
The means used to protect a resource to (for example) force serialisation is called a semaphore. There are numerous technical definitions for a semaphore. But they all imply the same thing. Apple calls it "A programming technique for coordinating activities in which multiple processes compete for the same kernel resources."
In Oracle, you can typically do this using DBMS_LOCK (as already stated). However, DBMS_LOCK is classified as a system procedure and by default, user/app schemas should not have access to it.
There is a very easy alternative to DBMS_LOCK - using Oracle's own default row locking.
This approach works in the following fashiion.
You create a dummy table (it will never contain any committed rows and only serve as the "semaphore device").
create table semaphore( id number primary key ) organization index;
To force serialisation, the very first step in the procedure is to insert a row into this table using a unique id for that procedure. E.g.
insert into semaphore values( UNIQUE_ID_FOR_MY_PROC );
A second (or 3rd/4th/etc) copy of the procedure will be blocked on its INSERT. It will wait for the 1st INSERT to be either committed or rolled back.
The procedure does its thing. Right at the end, it issues a:
delete from semaphore where id = UNIQUE_ID_FOR_MY_PROC;
This will still block the 2nd copy of the procedure. As soon as the 1st process now issue a COMMIT, the 2nd copy will continue and it will insert successfully into the SEMAPHORE table.
What I like about this approach is that any failure in the procedure, will result in ROLLBACK (either implicitly or explicitly). The ROLLBACK will remove the INSERT on the SEMAPHORE table and the 2nd copy of the procedure will now be able to continue.
Of course, it make sense not to do the INSERT and DELETE manually against the SEMAPHORE table, but instead use a PL/SQL package that does this for you. This way you have a nice wrapper API that can be extended to include more features.

Similar Messages

  • CS6 access to mySQL stored procedures in db panel?

    I'm using Dreamweaver CS5.5 and cannot access MySQL stored procedures in the database panel.  Anyone know if you can do this with CS6?

    This may help you: http://helpx.adobe.com/dreamweaver/kb/using-stored-procedures-within-dreamweaver.html#main _instantiating

  • Accessing Data from Stored Procedure using CallableStatement

    I have created a Stored Procedure as shown , IS it possible to access these data inside my JDBC Program ??
    CREATE or replace  PROCEDURE getEmpName(EMP_NUM IN NUMBER)
    is
    CURSOR C1 is select from emp ;*
    C2 c1%rowtype;
    BEGIN
    open c1;
    loop
    fetch c1 into c2;
    exit when c1%NOTFOuND;
    end loop;
    END ;
    *CallableStatement cs = conn.prepareCall("{call getEmpName(?)}");*

    What do you mean by :
    IS it possible to access these data inside my JDBC Program ??JDBC provide/helps to execute a stored proc. when we say executing a proc, it actually executes on DB. So if a proc is written which has in and out parameter. and if does some work based on in parameter and returns the result in out parameter. Here if you mean, like can i read the out parameter and check what it has returned, then yes you can access this data from JDBC.
    CREATE or replace PROCEDURE getEmpName(EMP_NUM IN NUMBER)
    is
    CURSOR C1 is select from emp ;*
    C2 c1%rowtype;
    BEGIN
    open c1;
    loop
    fetch c1 into c2;
    exit when c1%NOTFOuND;
    end loop;
    END ;
    /But in your above proc it has only in parameter, no out parameter and has only select. so you could execute the proc using JDBC.

  • Jena Adapter API access from Java Stored Procedures?

    Is it possible to use Jena API from code in java stored procedures in Oracle DBMS 11gR2? Possible but not recommended?
    The reason is that my company uses .Net as the application server plattform (not Java). Calling Java Stored Procedures from .Net is one way to use Jena library.
    Thank you!
    Trond

    Hi Trond,
    Unfortunately the sdordfclient.jar was compiled and built with JDK 6 and the JVM supported in the Oracle database 11gR2 is JDK5.
    If you have a strong requirement, please file a SR against Oracle support. We will then look into a possible recompilation and
    rebuild.
    Thanks,
    Zhe Wu

  • Problem accessing float in stored procedures

    I wrote a stored procedure that updates a column in a table whose datatype is of float(126). I declared a global variable in the stored procedure. The value of this variable is computed in the stored procedure and is used to update the table. My problem is an error message denoting my variable declaration to be at fault keeps on showing up even if I tried different datatypes in my declaration. I've tried RANK NUMBER, RANK NUMBER(19),
    RANK FLOAT(126) even RANK NUMBER(2,2). Can anyone help?

    Albert,
    I don't know what you mean by a "global" variable in a stored procedure. Is this a java stored procedure, or is it a PL/SQL stored procedure? If it is PL/SQL, perhaps the PL/SQL forum would be more appropriate?
    For your information, the following (PL/SQL) stored procedure works fine for me on a Oracle 9.2.0.4 database:
    create or replace procedure avi as
    fv float(126);
    begin
    fv := 1.7;
    dbms_output.put_line(to_char(fv));
    end;Perhaps you would care to provide some more information?
    [But I'm not guaranteeing that I will be able to resolve your issue, if you do.]
    What database version are you using?
    Is this a java stored procedure?
    Please post the entire error message (and stack trace?) you are getting.
    Please post the code that is causing the error. [Enough code so that I can try and reproduce your situation on my computer.]
    Good Luck,
    Avi.

  • Give execute access on SP_WHO2 stored procedure for a developer without giving VIEW SERVER STATE access.

    Hi,
    Is there anyway, we can give just execute access on sp_who2 SP for a developer without giving VIEW SERVER STATE access?
    Please let me know ASAP? Any help is greatly appreciated.
    Thanks,
    Vel
    Vel Thavasi

    Thanks Erland,
    I tried on master as well. Seems like no luck. I think, I am messing up with certificate or access level!! Would you mind in modify my script and help me out on this issue?
    CREATE
    CERTIFICATE uSPWHO2Cert
    ENCRYPTION BY PASSWORD
    = 'testpwd'
    WITH SUBJECT =
    'Certificate to run usp_who2',
    START_DATE = '20140901',
    EXPIRY_DATE =
    '20300101'
    go
    --drop certificate uSPWHO2Cert
    -- Create the certificate user and give it rights to access the test table.
    CREATE
    Login uViewServerStateUser
    FROM CERTIFICATE
    uSPWHO2Cert
    go
    create
    proc [usp_who2]
    as
    exec
    sp_who2;
    GO
    GRANT
    Execute ON
    usp_who2 TO uViewServerStateUser
    go
    -- Sign the procedure.
    ADD
    SIGNATURE TO
    usp_who2 BY CERTIFICATE
    uSPWHO2Cert
    WITH PASSWORD =
    'testpwd'
    go
    --drop proc usp_who2
    Grant
    View server
    State to uViewServerStateUser;
    Grant
    Execute on
    usp_who2 to testuser;
    -- Run as the test user, to actually see that this works.
    EXECUTE
    AS LOGIN
    = 'testuser'
    go
    -- First run the unsigned procedure. This gives a permission error.
    EXEC
    usp_who2
    go
    revert
    Thanks again!!!
    Thanks,
    Vel
    Vel Thavasi

  • Stored Procedure For MS Access

    I have searched the forum for answer to the problem in using stored procedure with MS Access. Basically, there is not much of JDBC and MS Access compatibility for stored procedure.
    The create procedure SQL in the JDBC tutorial does not work. I have tried so hard battling all the difficulties along the way (up to this point).
    Should I just forget about it, and move on to the next topic?
    I got errors in "create table"...in the following:
    String createProcedure = "create procedure SHOW_SUPPLIERS as select ....";
    Statement stme = con.createStatement ( );
    stmt.executeUpdate (createProcedure);

    can you send me the code snippet that does this call from java...i have written a query named "TestQuery" in MSAccess.
    Also which driver are you using, b'cuz when i tried to call that query like
    String sql = "{exec TestQuery}";
    CallableStatement cstmt = conn.prepareCall(sql,     ResultSet.TYPESCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    i got an exception, here is the exception trace........
    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]Optional feature not implemented
         at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6031)
         at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6188)
         at sun.jdbc.odbc.JdbcOdbc.SQLPrepare(JdbcOdbc.java:4182)
         at sun.jdbc.odbc.JdbcOdbcConnection.prepareCall(JdbcOdbcConnection.java:553)
         at com.neoforma.DiscTools.test.TestMSAccess.<init>(TestMSAccess.java:41)
         at com.neoforma.DiscTools.test.TestMSAccess.main(TestMSAccess.java:60)
    Regards,
    Anil

  • Stored Procedure/Function

    Hi All
    I am trying to create a stored procedure that will return a line from a table.
    The basic brief I have is that we have a bunch of tables that set up the user security and they link back to a our_systems table which holds info about all internal systems.
    I need a stored procedure which can be called with a username and a system id. If the user has rights to the system then the line from the our_system table will be returned along with a rights level.
    The hard part is I want to stop the users from seeing all the tables in this section. The original idea is to denie access to the tables but allow access to a stored procedure. Would this work?
    The second point is how can I get this working? I am used to MS SQL Server and this would easily be done by creating the procedure and then doing
    SELECT * FROM spCheckRights('dave',1)
    But Oracle (9i is the version I am using) says that a function / procedure can't be placed there.
    I have 2 version of my procedure
    FUNCTION spCheckRights (login_name In varchar2, drxsystem_id In number)
    return sys_refcursor
    AS
    l_cursor sys_refcursor;
    BEGIN
    open l_cursor for SELECT ds.NAME FROM drx_systems ds WHERE ds.system_id = 1;
    return l_cursor;
    END;
    OR
    FUNCTION spCheckRights (login_name In varchar2, drxsystem_id In number)
    return drx_systems%ROWTYPE
    AS
    drxsys drx_systems%ROWTYPE;
    BEGIN
    SELECT * INTO drxsys FROM drx_systems ds WHERE ds.system_id = 1;
    return drxsys;
    END;
    As you can see neither do all the check rights stuff yet, I wanted to test the basics first. Any views on which method is better for returning a row?
    Any help would be appreciated
    Thanks in advance
    Dave R

    As long as the owner of the function has rights to the tables, the users calling the function do not need to have any rights on the table, only execute on the function.
    The choice of using a procedure or a function (in Oracle they differ) and whether to return a cursor, a collection type or a record type really depends on your front-end requirements, and the number of values you want to return.
    A function can only return a single value. That value can be a cursor or a collection of some kind (think array), or it can be a scalar variable. A procedure can return, through out parameters, multiple values. This set of types that can be returned is the same as for a function.
    So, for a function returning a cursor, you would do something analagous to:
    curvar ResultSet;
    curvar = f(p1, p2);in the language appropriate way. For testing in sqlplus, that would be something like:
    VAR curvar SYS_REFCURSOR;
    exec :curvar := f(p1, p2);
    print curvar;Note, the colon in front of curvar in the exec is required. As long as your calling code can define a variable of drx_systems%ROWTYPE to recieve it, then the second version should work similarly.
    If you will only be returning values from a single row, and if the number of coumns is relatively small, you could also consider a procedure something like:
    PROCEDURE spcheckrights (p_login_name   IN VARCHAR2,
                             p_drxsystem_id IN NUMBER,
                             p_name         OUT VARCHAR2,
                             p_allowed      OUT VARCHAR2) AS
    BEGIN
       SELECT ds.name, ds.allowed
       INTO p_name, p_allowed
       FROM drx_systems
       WHERE system_id = 1;
    END;Then call it passing in variable to get the results. Again, in sqlplus that would look something like:
    VAR name VARCHAR2(30);
    VAR allowed VARCHAR2(1);
    exec p(p1, p2, :name, :allowed);
    print name, allowed;Again, in sqlplus, the colons before name and allowed in the exec call are required.
    HTH
    John

  • Failure of EXECUTE IMMEDIATE when in a stored procedure

    Hello,
    this concerns behaviour observed in Oracle 10g on Windows.
    As user "system", I execute from the command line the following:
    SQL> select COUNT(1) from sys.dba_sequences;
    COUNT(1)
    645
    Again I try the same thing from the command line:
    SQL> declare
    2 CNT INTEGER:=0;
    3 STMT VARCHAR2(300);
    4 BEGIN
    5 STMT:='select COUNT(1) from SYS.DBA_SEQUENCES';
    6 EXECUTE IMMEDIATE STMT INTO CNT;
    7 dbms_output.put_line('CNT='||CNT);
    8 end;
    9 /
    CNT=645
    So far so good, clearly this table exists and I have sufficient privileges to access SYS.DBA_SEQUENCES as user system.
    However, if I put this in a stored procedure in a script TEST_SEQ.sql and then execute, I get ORA-00942:
    1. Create or replace Procedure TEST_A1
    2. IS
    3. CNT INTEGER:=0;
    4. STMT VARCHAR2(300);
    5.
    6. BEGIN
    7. STMT:='select COUNT(1) from SYS.DBA_SEQUENCES';
    8.     EXECUTE IMMEDIATE STMT INTO CNT;
    9.     dbms_output.put_line('CNT='||CNT);
    10. END;
    SQL> @ TEST_SEQ.sql
    SQL> exec TEST_A1;
    ORA-00942: Table or view does not exist
    ORA-06512: At "SYSTEM.TEST_A1", line 8
    ORA-06512: At line 1
    Why now does this table not seem to exist when accessed from a stored procedure?

    http://www.google.com/search?btnG=1&q=ora-0942+procedure
    Sybrand Bakker
    Senior Oracle DBA
    Experts: those who do read documentation.

  • Java App connection to an Oracle Database through Oracle Stored Procedure

    My team's access to its Databases (Oracle only) is restricted to access through Oracle Stored Procedures that are part of the Oracle Database. So, in my Java App I need to call the Stored Procedure that will give me the access to the table that I need, declare the right parameters and execute the command. The Stored Procedure will then hand me the data I need.
    I am finding support on the web for creating Stored Procedures in Java, but that is not what I need.
    Can anyone post here a class that addresses this, or point me to a link that will shed some light on it?
    Thanks
    user606303

    user606303 wrote:
    Sorry this code is unformatted - I can't see how to format it.Use \ tags
    I am looking for Java code that will do what this .NET code below does (connects to a database and writes data to the table through an Oracle stored procedure that is part of the Oracle Database.)
    So learn Java, learn JDBC and translate the requirements; don't attempt to translate the code as the platforms are too different.
    From a quick glance it looks like a JDBC CallableStatement can do the job.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Grant read only permission on my stored procedure.

    I have a requirement like give reaonly access on my stored procedure to another user , not even execute permission on that steored procedure.
    Could you please let us know the command ?

    Marwim wrote:
    You can read the source of any PL/SQL code in dba_sourceBut that requires a priv such as select any dictionary to be granted. Why would you want to give a schema access to reading any and all source code in the database?
    This is why I think it is important that the OP provides the reasons behind the question of granting read-only source code access.
    Security is a critical component of software engineering. The basic security principle is to grant the absolute minimum privileges required to s/w and users to get the job done. Granting access to a schema read access to a dictionary view like DBA_SOURCE violates it.
    If userB wants to see userA's source code - then why not have userA simply mail it to userB, or check the code into a common source code repository?

  • Unable to access Custom UDTs returned from a Java Stored Procedure

    Hi,
    I have a UDT in the DB :-
    create type contactrecord as object (
    CN_ID NUMBER(8),
    CN_TITLE VARCHAR2(40),
    CN_FIRST_NAME VARCHAR2(25)
    and this is the corresponding java class ContactDetails.java that maps to this UDT, that I loaded in the Aurora VM.
    package package1;
    mport java.sql.SQLData;
    import java.sql.SQLException;
    import java.sql.SQLInput;
    import java.sql.SQLOutput;
    public class ContactDetails implements SQLData
    private String sql_type;
    private long CN_ID;
    private String CN_TITLE;
    private String CN_FIRST_NAME;
    public String getSQLTypeName() throws SQLException
    return this.sql_type;
    //implementation of readSql
    public void readSQL(SQLInput stream, String typeName) throws SQLException
    sql_type = typeName;
    CN_ID = stream.readLong();
    CN_TITLE = stream.readString();
    CN_FIRST_NAME = stream.readString();
    public void writeSQL(SQLOutput stream) throws SQLException
    stream.writeLong(CN_ID);
    stream.writeString(CN_TITLE);
    stream.writeString(CN_FIRST_NAME);
    //getters and setters for the class vars go here.....
    There is another class A.java that has a java stored procedure/function, which I loaded into the Aurora VM
    Here is the class.
    package package1;
    public class A
    public static ContactDetails returnObject(String name )
         ContactDetails cd = new ContactDetails();
         cd.setCN_ID(1);
    cd.setCN_FIRST_NAME(name);
    return cd;
    Then I declared the call spec for A.returnObject() as
    FUNCTION returnObject(name varchar2) return contactrecord
    AS LANGUAGE JAVA
    NAME 'package1.A.returnObject(java.lang.String) return package1.ContactDetails';
    Then I tried to call the function returnObject through JDBC calls from a class in another VM.
    When I access the object returned by the function, I get a null object.
    Here is the Client code:
    CallableStatement cs = null;
    ResultSet rs = null;
    try
    cs = conn.prepareCall("{ ? = call returnObject(?) }");
    java.util.Map map = conn.getTypeMap();
    map.put("ADMIN.CONTACTRECORD", Class.forName("package1.ContactDetails"));
    conn.setTypeMap(map);
    cs.registerOutParameter(1, OracleTypes.STRUCT, "ADMIN.CONTACTRECORD");
    cs.setString(2, "John Doe" );
    cs.execute();
    ContactDetails cd = (ContactDetails)cs.getObject(1);
    System.out.println("contact first name is:-"+cd.getCN_FIRST_NAME()); //Null Pointer here..cd is null....:(
    if (cs != null) cs.close();
    catch(Exception e)
    e.printStackTrace();
    Although If I try to access the same function from a pl/sql block, I am able
    to access the contactrecord fields.
    What could be wrong ..???
    I could not find any error with the object mapping, as it works perfectly when I interact directly from my VM to the DB,
    without going thru the aurora VM.
    I am using a OCI driver to connect to the DB via JDBC.
    Thanx in advance for any help at all.
    -sk

    Shahid,
    I too have had bad luck in many cases with the automatic translation of Java types to PL/SQL and back. I think the SYS package on the PL/SQL side which handles some of the conversion is DBMS_PICKLER (there are equivalent Java classes which do the same in that world and seem to execute automagically when a conversion is needed). You might want to double-check the data type mappings against the DOC on OTN to make sure they map 1-1. Also make sure the permissions are granted against your objects to whoever is executing them, etc. Very often, I've resorted to passing simple scalar types between the two languages as in some cases the results with complex types are inconsistent.
    Sorry this isn't much help,
    -Dan
    http://www.compuware.com/products/devpartner/db/oracle_debug.htm
    Debug PL/SQL and Java in the Oracle Database

  • How do I retrieve a variable from a stored procedure in Access?

    Given a stored procedure like the one below,
    PROCEDURE getSomething(
                   pVar1           in float,
                   pVar2               in float,
                   pVar3               in float,
                   pVar4               in float,
                   pResults          out varchar2
    How would I retrieve the value in pResults using VBA on Access? Thanks.

    Using this, you can set the value to the session bean
    <c:set property="docID" target="${SessionBean1}" value="${pg_view_doc.hiddenField3.value}"/>
    But before that you have to create a property called docid in the sessionbean1.
    So once the property is set, through getDocID() method u can retrieve the value
    ex: String value=getSessionBean1().getDocID();
    will return the value in the required JSF Page bean

  • Can I create a Stored Procedure That access data from tables of another servers?

    I'm developing a procedure and within it I'm trying to access another server and make a select into a table that belongs to this another server. When I compile this procedure I have this error message: " PLS-00904: insufficient privilege to access object BC.CADPAP", where BC.CADPAP is the problematic table.
    How can I use more than one connection into an Oracle Stored Procedure?
    How I can access tables of a server from a Stored Procedure since the moment I'm already connected with another server?
    Can I create a Stored Procedure That access data from tables of another servers?

    You need to have a Database Link between two servers. Then you could do execute that statement without any problem. Try to create a database link with the help of
    CREATE DATABASE LINK command. Refer Document for further details

  • How to change Access Mode of HANA Stored procedure

    Hi,
    When you are creating a stored procedure as design time object, one of the option is access mode (either read only or read/write).
    I understand read only is default setting in HANA DB, and unless you explicitly change the setting you cannot read/write using stored procedure.
    My question is, is there way to change this setting, so I have option to choose either read or read/write when I create a stored proc?
    Thank you.
    Hyun

    Hi Hyun,
    Please have a look on this thread:
    Create local temporary table in procedure
    You have to enable sqlscript_mode to UNSECURE as mentioned by lars.
    Then depending on whether you are specifiying "READS SQL DATA" it will act as a READ procedure else as a WRITE procedure.
    Regards,
    Krishna Tangudu

Maybe you are looking for