简体   繁体   English

oracle 表插入事务日志

[英]oracle table insert transaction log

I have an application that is built on top of a framework;我有一个构建在框架之上的应用程序; the framework handles all the communication and requests to/from the DB.该框架处理所有进出数据库的通信和请求。 I have Oracle 12c.我有 Oracle 12c。

The application is an electronic document management system;该应用程序是一个电子文档管理系统; I have one document when trying to commit it will generate an error, the error is not that self-explanatory as begin controlled by the framework, the error related to the DB commit process.我有一个文档在尝试提交时会产生错误,该错误不像框架控制的那样不言自明,该错误与数据库提交过程有关。 I tried to check the constraint/Index for that specific table related to the eDoc with no luck.我试图检查与 eDoc 相关的特定表的约束/索引,但没有成功。

How I can get more log from oracle itself when inserting the data, hoping I can get a better idea of why the error is happening.如何在插入数据时从 oracle 本身获得更多日志,希望我能更好地了解错误发生的原因。

Regards问候

If you have a test database that you can reproduce the error in, you can create an AFTER SERVERERROR trigger to log any errors that occur.如果您有一个可以在其中重现错误的测试数据库,则可以创建一个AFTER SERVERERROR触发器来记录发生的任何错误。

If you try this in production, you risk large-scale problems for all users and applications accessing the database, especially if you make a mistake.如果您在生产环境中尝试这样做,您将面临所有访问数据库的用户和应用程序出现大规模问题的风险,尤其是在您犯了错误的情况下。 Doing this in a dev/QA instance is strongly recommended!强烈建议在 dev/QA 实例中执行此操作!

Here is a short example of how to do that:下面是一个简短的例子来说明如何做到这一点:

-- Create a table to hold the error log
CREATE TABLE matt1_errors (
    error_ts        TIMESTAMP,
    stacktrace      VARCHAR2(4000),
    sql_stmt        VARCHAR2(4000)); 
/

-- Create a trigger to log any server errors that occur
CREATE TRIGGER matt_log_errors AFTER SERVERERROR ON DATABASE
DECLARE
  l_sql_pieces_list DBMS_STANDARD.ora_name_list_t;
  piece_count PLS_INTEGER;
  l_sql_text VARCHAR2(4000);
BEGIN
  piece_count := ora_sql_txt(l_sql_pieces_list);

  FOR i IN 1..piece_count LOOP
    l_sql_text := substr(l_sql_text || l_sql_pieces_list(i), 1, 4000);
  END LOOP;

  INSERT INTO matt1_errors ( error_ts, stacktrace, sql_stmt)
     VALUES (SYSTIMESTAMP, dbms_utility.format_error_stack, l_sql_text);

EXCEPTION 
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE (dbms_utility.format_error_stack);
END;

Test it...测试一下...

-- Create a table to insert into.
CREATE TABLE matt1 ( id NUMBER, text VARCHAR2(4) );
-- Cause an error.
insert into matt1 (id, text) VALUES (1, 'Too long!!!');

-- See what we got...
select * from matt1_errors;


+---------------------------------+--------------------------------------------------------------------------------------+--------------------------------------------------------+
|            ERROR_TS             |                                      STACKTRACE                                      |                        SQL_STMT                        |
+---------------------------------+--------------------------------------------------------------------------------------+--------------------------------------------------------+
| 06-NOV-19 03.02.18.083116000 PM | ORA-12899: value too large for column "APPS"."MATT1"."TEXT" (actual: 11, maximum: 4) | insert into matt1 (id, text) VALUES (1, 'Too long!!!') |
+---------------------------------+--------------------------------------------------------------------------------------+--------------------------------------------------------+

So,所以,

  1. Create the AFTER SERVERERRORS trigger创建AFTER SERVERERRORS触发器
  2. Reproduce your error重现你的错误
  3. Check the table that your trigger writes to检查触发器写入的表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM