[ 
https://issues.apache.org/jira/browse/DERBY-6726?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15536681#comment-15536681
 ] 

Bernd Ruehlicke edited comment on DERBY-6726 at 9/30/16 6:48 PM:
-----------------------------------------------------------------

OK - after 7 hours debugging the reason is found - Bryan your instincts serve 
you well ... The main error is in TriggerEventActivator.

for 10.12.1.1 it is in the line 86 which has
tableName = triggerInfo.triggerArray[0].getTableDescriptor().getQualifiedName();

Note that the triggerInfo.triggerArray is HARDCODED to use index 0. Now this 
gives the needed tableName, BUT the "getTableDiscriptor()" method in 
TriggerDescriptor is actually a SETTER !!! It SETS  td if td is null,  That's 
why it works when just one trigger is in the test code and fails if 2 as the 
second trigger's td is never set. This bug must have been here for a long time 
and never surfaced.

So,  in the TriggerEventActivator line 86 the triggerInfo.triggerArray has 
correctly the 2 triggers but onlyindex 0 gets its "td" set via the 
"getTableDescriptor()".  To test this just add code which will loop for the 
length of the triggerArray and call the getTableDescritptor on each element.

I.e;  HACK to make it work (assuming the tableName is the same for each of the 
triggerInfo TableDescription ), replace line 86 with this chunk of code and it 
will work, it is of course the "setting" behavior of the getTableDescriptor 
which is the ugly part here,

tableName = triggerInfo.triggerArray[0].getTableDescriptor().getQualifiedName();
for(int i=1;i<triggerInfo.triggerArray.length;i++){
      triggerInfo.triggerArray[i].getTableDescriptor();
}







was (Author: bruehlicke):
OK - after 7 hours debugging the reason is found - Bryan your instincts serve 
you well ... The main error is in TriggerEventActivator.

for 10.12.1.1 it is in the lien 86 which has
tableName = triggerInfo.triggerArray[0].getTableDescriptor().getQualifiedName();

Note that the triggerInfo.triggerArray is HARDCODED to use index 0. Now this 
gives the needed tableName, BUT the "getTableDiscriptor()" method in 
TriggerDescriptor is actually a SETTER !!! It SETS  td if td is null,  That's 
why it works when just one trigger is in the test code and fails if 2 as the 
second trigger's td is never set. This bug must have been here for a long time 
and never surfaced.

So,  in the TriggerEventActivator line 86 the triggerInfo.triggerArray has 
correctly the 2 triggers but onlyindex 0 gets its "td" set via the 
"getTableDescriptor()".  To test this just add code which will loop for the 
length of the triggerArray and call the getTableDescritptor on each element.

I.e;  HACK to make it work (assuming the tableName is the same for each of the 
triggerInfo TableDescription ), replace line 86 with this chunk of code and it 
will work, it is of course the "setting" behavior of the getTableDescriptor 
which is the ugly part here,

tableName = triggerInfo.triggerArray[0].getTableDescriptor().getQualifiedName();
for(int i=1;i<triggerInfo.triggerArray.length;i++){
      triggerInfo.triggerArray[i].getTableDescriptor();
}






> NPE from trigger
> ----------------
>
>                 Key: DERBY-6726
>                 URL: https://issues.apache.org/jira/browse/DERBY-6726
>             Project: Derby
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 10.10.1.1
>            Reporter: Tim Dudgeon
>         Attachments: TriggerTest.diff, derbytrig.zip
>
>
> Saw this strange exception when doing an insert to a table with a trigger
> {code}
> Tue Sep 02 13:39:09 BST 2014 Thread[SQLExecution,1,system] (XID = 62693), 
> (SESSIONID = 1), (DATABASE = 
> C:/Users/timbo/Documents/IJCProjects/mini-regs/Vanilla 
> Oracle/.config/derby-minireg-01-sep/db), (DRDAID = null), Failed Statement 
> is: UPDATE samples SET sample_code = 'S123456' WHERE sample_id = CAST 
> (org.apache.derby.iapi.db.Factory::getTriggerExecutionContext().getNewRow().getObject(1)
>  AS INTEGER)
> java.lang.NullPointerException
>     at 
> org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getTriggerActionString(Unknown
>  Source)
>     at 
> org.apache.derby.iapi.sql.dictionary.TriggerDescriptor.getActionSPS(Unknown 
> Source)
>     at 
> org.apache.derby.impl.sql.execute.GenericTriggerExecutor.getAction(Unknown 
> Source)
>     at 
> org.apache.derby.impl.sql.execute.RowTriggerExecutor.fireTrigger(Unknown 
> Source)
>     at 
> org.apache.derby.impl.sql.execute.TriggerEventActivator.notifyEvent(Unknown 
> Source)
>     at 
> org.apache.derby.impl.sql.execute.UpdateResultSet.fireAfterTriggers(Unknown 
> Source)
>     at org.apache.derby.impl.sql.execute.UpdateResultSet.open(Unknown Source)
>     at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown 
> Source)
>     at 
> org.apache.derby.impl.sql.GenericPreparedStatement.executeSubStatement(Unknown
>  Source)
>     at 
> org.apache.derby.impl.sql.execute.GenericTriggerExecutor.executeSPS(Unknown 
> Source)
>     at 
> org.apache.derby.impl.sql.execute.RowTriggerExecutor.fireTrigger(Unknown 
> Source)
>     at 
> org.apache.derby.impl.sql.execute.TriggerEventActivator.notifyEvent(Unknown 
> Source)
>     at 
> org.apache.derby.impl.sql.execute.InsertResultSet.normalInsertCore(Unknown 
> Source)
>     at org.apache.derby.impl.sql.execute.InsertResultSet.open(Unknown Source)
>     at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown 
> Source)
>     at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown 
> Source)
>     at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown 
> Source)
>     at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
>     at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
> {code}
> The trigger definition is this:
> {code}
> CREATE TRIGGER samples_code_trg
> AFTER INSERT ON samples
> REFERENCING NEW AS newrow FOR EACH ROW MODE DB2SQL
> UPDATE samples SET sample_code = 'S123456'
> WHERE samples.sample_id = newrow.sample_id;
> {code}
> As mentioned here: 
> http://mail-archives.apache.org/mod_mbox/db-derby-user/201408.mbox/%[email protected]%3E
> it could be that its caused by another AFTER UPDATE trigger that's on the 
> table.
> Unfortunately I rebuilt all the tables and triggers and not the problem 
> doesn't happen, so I can't provide a test case.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to