Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
LOCK Table from DML operations

Hi Experts,
I have a requrement, want my table to lock until some operations with in table creations are done. (Ex : altering fields, data types etc.. thru an automation). During the table creation in case of any failures occurs, table should be in locked from all DML operation for all users.
Thanks
Answers
-
So, you mean that a table has to be locked, but, in the mean time, there must be going on some transactions, including commits and rollbacks.
The only way I can think of is something that goes along the lines below:
execute immediate 'lock table x_table in exclusive mode';
--- call procedures that run as autonomous transactions (having pragma autonomous_transaction) so that they may commit without having the lock on that table released even for a fraction of a second
commit;
-
table should be in locked from all DML operation for all users
alter table tbl read only; .... alter table tbl read write;
To make some "alter", you may need DML. For example, when adding a not null column without default value.
Use EBR to make changes temporary invisible to the application.
-
lock_table is session specific and once session is closed lock will also be released and other users will be able to perform DMLs.
A better option would be to make table read only before ALTER table ( DDL Commands) and if DDLs are successful make it read-write else it remains read only (even session is closed)
BEGIN execute immediate 'alter table table_name read only'; --Perform Your DDL Commands here. execute immediate 'alter table table_name read write'; END; /
Note that this does not stop users from selecting data from table.
Regards
Arun
-
execute immediate 'lock table x_table in exclusive mode';
The locked table cannot be altered "Ex : altering fields, data types etc.."
Why "execute immediate"?
-
In case you dont want to make your DDLs dynamic (Dont want to use EXECUTE IMMEDIATE)
use below in sql script.
WHENEVER SQLERROR THEN EXIT
Regards
Arun
-
So, any DDL on a table locks the whole table while it is executed and it also commits at the end. Kind of you can't both have your cake and eat it at the same time. Once the DDL finished nothing would prevent other sessions from performing some DML (insert/update/delete) on that table.
I thought you wanted to have a table locked while you perform some DDL on other table(s).
Or, another way to implement some kind of locking mechanism in order to prevent process X to start while process Y is running is to use the facilities of the dbms_lock package (having the release_on_commit false allows commits and rollbacks in the session which acquired the lock, unlike lock table or select for update, which releases the lock on commit or update). But dbms_lock is far from being something that may prevent any session from performing some actions and may be used only for very specific purposes.
I think your issue is that the schemas, users, objects and grants are not well designed for what you actually need and that leads you to put the problem the way you do.
So, the classic way for handling such issues is to have the tables for which you wish the operations to be restricted under some conditions placed in a schema, and in that schema, have some packages that provide stored procedures which actually include the DML on those tables, and all the other users would not have grants on the tables themselves, but on those packages. So you may enforce complex business logic and required processing flows via PL/SQL packages.
-
Depending on what you want to actually do (in detail) you could also use the renaming of tables... i.e. table is renamed to another name (effectively table no longer exists to users), amendments are done on the renamed table and then, once complete the table is renamed back to the original name.
Of course that can have complications if there are referential constraints and suchlike, but without knowing the detail or how complex a task is actually required it's hard to say.
Usually though, if updates are required that include preventing user access, then you have scheduled 'downtime' on the system, where users are prevented from accessing the entire database (through whatever means is most suitable) and then only brought back 'online' for users when the updates are complete.
-
I have a requrement, want my table to lock until some operations with in table creations are done. (Ex : altering fields, data types etc.. thru an automation). During the table creation in case of any failures occurs, table should be in locked from all DML operation for all users.
Something doesn't add up. If any failurte occurs while creating table then table is not created and nobody can issue DML/DDL against it. In fact nobody can ssue DML/DDL against it even while table is being created. Session 1:
drop table test purge / create table test(id number) / create or replace trigger after_alter after alter on database begin if ora_dict_obj_owner = 'SCOTT' then dbms_lock.sleep(60); end if; end; / alter table test add name varchar2(10) /
So we wait 60 seconds before completing ALTER to mimc situation where DML is issued while we are altering table. And while we alter the table Session 2:
insert into test values(1) / into test * ERROR at line 2: ORA-00947: not enough values Elapsed: 00:00:50.31 SQL>
As you can see, session 2 waited till ALTER is done and then failed since now tabke has 2 columns.
Anyway, you need to explain in detail what are you trying to do.
SY.