How to know which row in a table is changed using java and postgresql -
i have following class listens notifications postgresql database.
class listener extends thread { private connection conn; private org.postgresql.pgconnection pgconn; listener(connection conn) throws exception { this.setconn(conn); this.pgconn = (org.postgresql.pgconnection) conn; statement stmt = conn.createstatement(); stmt.execute("listen userdata"); stmt.close(); } public connection getconn() { return conn; } public void setconn(connection conn) { this.conn = conn; } public static void main(string[] args) { try { class.forname("org.postgresql.driver"); string url = "jdbc:postgresql://localhost/users"; string user = "admin"; string password = "password"; connection listenercon = drivermanager.getconnection(url, user, password); listener listener = new listener(listenercon); listener.start(); } catch (exception e) { e.printstacktrace(); } } public void run() { while (true) { try { statement stmt = conn.createstatement(); resultset rs = stmt .executequery("select * userdata id='1'"); rs.close(); stmt.close(); org.postgresql.pgnotification notifications[] = pgconn .getnotifications(); if (notifications != null) { (int = 0; < notifications.length; i++) { system.out.println("got notification: " + notifications[i].getparameter()); } } thread.sleep(500); } catch (exception e) { e.printstacktrace(); } } } }
these following columns in userdata table:
id cn sn givenname c l st street o ou title description postaladdress telephonenumber email userpassword adorldap
here stored procedure in users database
ccreate or replace function get_effected_row_primarykey() returns trigger $$ declare primarykey text; begin if (tg_op='update') if old.cn <> new.cn select pg_notify('userdata', 'cn'); end if; if old.sn <> new.sn select pg_notify('userdata', 'sn'); end if; if old.givenname <> new.givenname select pg_notify('userdata', 'givenname'); end if; if old.c <> new.c select pg_notify('userdata', 'c'); end if; if old.l <> new.l select pg_notify('userdata', 'l'); end if; if old.st <> new.st select pg_notify('userdata', 'st'); end if; if old.street <> new.street select pg_notify('userdata', 'street'); end if; if old.o <> new.o select pg_notify('userdata', 'o'); end if; if old.ou <> new.ou select pg_notify('userdata', 'ou'); end if; if old.title <> new.title select pg_notify('userdata', 'title'); end if; if old.description <> new.description select pg_notify('userdata', 'description'); end if; if old.postaladdress <> new.postaladdress select pg_notify('userdata', 'postaladdress'); end if; if old.telephonenumber <> new.telephonenumber select pg_notify('userdata', 'telephonenumber'); end if; if old.email <> new.email select pg_notify('userdata', 'email'); end if; if old.userpassword <> new.userpassword select pg_notify('userdata', 'userpassword'); end if; if old.adorldap <> new.adorldap select pg_notify('userdata', 'adorldap'); end if; return new; elsif (tg_op='delete') select pg_notify('userdata', old.id::text); return old; elsif (tg_op='insert') select pg_notify('userdata', new.id::text); return new; end if; return null; end; $$ language plpgsql;
here trigger
create trigger create_data after insert or update or delete on userdata each row execute procedure get_effected_row_primarykey();
java program above works fine, gets notified whenever there insert, update, or delete in table. questions are:
updated stored procedure. when insert table, stored procedure returning following error:
error: query has no destination result data hint: if want discard results of select, use perform instead. context: pl/pgsql function get_effected_row_primarykey() line 59 @ sql statement
can resolve it.any appreciated
in pl/pgsql function, when issue select
command, need specify put result, even when no rows returned.
so in case replace all
select pg_notify('userdata', ...);
with
perform pg_notify('userdata', ...);
Comments
Post a Comment