From: | johann at myrkraverk(dot)com (Johann 'Myrkraverk' Oskarsson) |
---|---|
To: | |
Subject: | [Pljava-dev] Proper error and resource handling in triggers throwing exceptions? |
Date: | 2010-10-08 22:52:08 |
Message-ID: | AANLkTinGR3pnhKRdGvFFFs-s-=qwPz_4q3vCKyn+Zh=F@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pljava-dev |
Hi pljava,
Since there is no proper user forum (that I am aware of) I ask here.
In a trigger that explicitly throws an exception, what is the proper
way to close resources?
In an attempt to do it properly I came up with this:
public static void isAllowed( TriggerData td )
throws SQLException
{
Connection conn = null;
PreparedStatement lookup = null;
ResultSet r = null;
try {
// Errorchecks begin
if ( td.isFiredForStatement() )
throw new TriggerException( td, "can't process STATEMENT events" );
if ( td.isFiredAfter() )
throw new TriggerException( td, "must be fired before event" );
if ( !td.isFiredByUpdate() )
throw new TriggerException( td, "can only process UPDATE events" );
// Errorchecks end
String[] arguments = td.getArguments();
if ( arguments.length != 1 )
throw new TriggerException( td, "Exactly one argument required." );
String column = arguments[ 0 ];
conn = DriverManager.getConnection( "jdbc:default:connection" );
lookup = conn.prepareStatement(
"SELECT value FROM is_allowed WHERE name = ? and value = ?;" );
ResultSet n = td.getNew();
String value = n.getString( column );
lookup.setString( 1, column );
lookup.setString( 2, value );
r = lookup.executeQuery();
if ( !r.next() )
{
throw new TriggerException( td, "Value '" + value
+ "' not allowed." );
}
} finally {
if ( r != null ) r.close();
if ( lookup != null ) lookup.close();
if ( conn != null ) conn.close();
}
}
It takes a single column name as argument and looks the new value up
in the table is_allowed which has the tubles ( 'column name', 'allowed
value' ). Everything is TEXT.
Is this enough or should I do the bloatware explained here:
http://dmlloyd.blogspot.com/2008/07/proper-resource-management.html
Johann
From | Date | Subject | |
---|---|---|---|
Next Message | Johann 'Myrkraverk' Oskarsson | 2010-10-09 21:39:21 | [Pljava-dev] PL/Java 1.5.0 Milestones |
Previous Message | Johann 'Myrkraverk' Oskarsson | 2010-10-08 15:03:45 | [Pljava-dev] 1.4.1 Release Schedule: Friday 29th of October with Java 6 & PostgreSQL 8.4 and 9.0 |