Обсуждение: getNotifications

Поиск
Список
Период
Сортировка

getNotifications

От
kfan
Дата:
I'm a newbie to postgreSQL so please forgive my ignorance.  I am able to
write code that connects/updates a database. I copied a simple Notify/Listen
example and for the life of me I can't seem to figure out why the
getNotifications() method for the PGConnection class is undefined?  All the
examples use it and the docs say its available after version 7.3 or so.
I've installed PostgreSQL 9.1.4, and link to postgresql-9.0-801.jdbc4.jar in
my project.  What piece am I missing?



--
View this message in context: http://postgresql.1045698.n5.nabble.com/getNotifications-tp5720228.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


Re: getNotifications

От
Maciek Sakrejda
Дата:
It would help to see your code and the exception you're getting. My
guess is you're not casting the Connection object you get back from
DriverManager to a PGConnection, but that's a shot in the dark at this
point.


Re: getNotifications

От
kfan
Дата:
Thank you for the assistance.

It's not an exception, it's a compile error.  I am casting the connection
object to a PGConnection...interesting to note though that the methods
available on the PGConnection object only shows the addDatatype method.

the error: the method getNotifications() is undefined for type PGConnection.

What I see now is PGNotification is not available but PGConnection is...not
sure I saw that earlier.  Where is that class? Thought it was part of the
same package.

The code is something I found online:


import java.sql.*;

...
  Listener(Connection conn) throws SQLException {
      this.conn = conn;
      this.pgconn = (org.postgresql.PGConnection)conn;
      Statement stmt = conn.createStatement();
      stmt.execute("LISTEN mymessage");
      stmt.close();
   }

   public void run() {
      while (true) {
         try {

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT 1");
            rs.close();
            stmt.close();

            org.postgresql.PGNotification notifications[] =
pgconn.getNotifications();
            if (notifications != null) {
               for (int i=0; i<notifications.length; i++) {
                  System.out.println("Got notification: " +
notifications[i].getName());
               }
            }
...





--
View this message in context: http://postgresql.1045698.n5.nabble.com/getNotifications-tp5720228p5720235.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


Re: getNotifications

От
Maciek Sakrejda
Дата:
On Fri, Aug 17, 2012 at 9:23 AM, kfan <kfan.b1@gmail.com> wrote:
> It's not an exception, it's a compile error.  I am casting the connection
> object to a PGConnection...interesting to note though that the methods
> available on the PGConnection object only shows the addDatatype method.
>
> the error: the method getNotifications() is undefined for type PGConnection.

That's odd, since getNotifications() was there before addDatatype if
I'm reading the history right.

> What I see now is PGNotification is not available but PGConnection is...not
> sure I saw that earlier.  Where is that class? Thought it was part of the
> same package.

It is. What version of the driver are you using? Also, it would be
useful to see a full (and, ideally, minimal) test case and the exact
compilation errors you're getting.

> The code is something I found online:

Seems reasonable at first glance...


Re: getNotifications

От
dmp
Дата:
kfan wrote:
> On Fri, Aug 17, 2012 at 9:23 AM, kfan<kfan.b1@gmail.com>  wrote:
>> It's not an exception, it's a compile error.  I am casting the connection
>> object to a PGConnection...interesting to note though that the methods
>> available on the PGConnection object only shows the addDatatype method.

Maciek Sakrejda wrote:
> It would help to see your code and the exception you're getting. My
> guess is you're not casting the Connection object you get back from
> DriverManager to a PGConnection, but that's a shot in the dark at this
> point.

Listener(Connection conn) throws SQLException {
       this.conn = conn;
       this.pgconn = (org.postgresql.PGConnection)conn;

I'm not sure, but I think I'm with Maciek's orginally response. I don't
think you're going to be able to cast a java.sql.Connection to PGConnection
then call one of the latter's methods.

danap.



Re: getNotifications

От
Maciek Sakrejda
Дата:
> I'm not sure, but I think I'm with Maciek's orginally response. I don't
> think you're going to be able to cast a java.sql.Connection to PGConnection
> then call one of the latter's methods.

You can, if that's the actual runtime type of the Connection object.
You get a Connection back from DriverManager, so it has to work like
that. If there's a pooler in the way things can get more hairy (i.e.,
PGConnection can be wrapped in something that implements
java.sql.Connection but is not a PGConnection, and that can end up as
the only thing expose to the user), but k f was talking about a
compile-time error.


Re: getNotifications

От
Maciek Sakrejda
Дата:
Please keep the list copied: if I stop feeling helpful, maybe someone
else can pick it up ;)

Your code is correct:

maciek@gamera:~/Downloads$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.3) (6b24-1.11.3-1ubuntu0.12.04.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
# the above means we'll build a jdbc4 jar
maciek@gamera:~/code/aux/pgjdbc$ git checkout REL9_0_801
...
maciek@gamera:~/code/aux/pgjdbc$ ant clean jar
...
maciek@gamera:~/Downloads$ javac -cp
/home/maciek/code/aux/pgjdbc/jars/postgresql.jar NotificationTest.java
maciek@gamera:~/Downloads$ java -cp
/home/maciek/code/aux/pgjdbc/jars/postgresql.jar:. NotificationTest
testing...
Got notification: mymessage
testing...
testing...
Got notification: mymessage

I think it's maybe a classpath issue? How is your environment set up /
how are you referencing the postgresql.jar (yes, that's the driver)?


Re: getNotifications

От
kfan
Дата:
Maciek - I responded directly to your email and attached my test code.

I am a bit confused as I removed the "details" from the run method so it
would compile, then tried to run, baby steps, and get the following runtime
error:

Exception in thread "main" java.lang.ClassNotFoundException:
org.postgresql.Driver on the line

      Class.forName("org.postgresql.Driver");

I am using Eclipse, my build path has the postgresql-9.0-801.jdbc4.jar. I
know it must be something simple.  I've done a little googling and not sure
I am getting it.



--
View this message in context: http://postgresql.1045698.n5.nabble.com/getNotifications-tp5720228p5720244.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


Re: getNotifications

От
kfan
Дата:
I am guessing it is a classpath issue as well...just not sure what it is or
how to resolve it.  I am running within the Eclipse environment for now and
I have specified the postgresql-9.0-801.jdbc4.jar in the build path library
list.



--
View this message in context: http://postgresql.1045698.n5.nabble.com/getNotifications-tp5720228p5720246.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


Re: getNotifications

От
Maciek Sakrejda
Дата:
On Fri, Aug 17, 2012 at 10:22 AM, kfan <kfan.b1@gmail.com> wrote:
> Maciek - I responded directly to your email and attached my test code.

It's fine to send small attachments to the list. Like I said, this is,
among other things, a place to get help, but the best way to do that
is to keep your questions directed to the list, not individuals. It's
easier on the contributors and makes it more likely you'll get help.

> I am guessing it is a classpath issue as well...just not sure what it is or
> how to resolve it.  I am running within the Eclipse environment for now and
> I have specified the postgresql-9.0-801.jdbc4.jar in the build path library
> list.

I haven't used Eclipse for a while, but I think build-time
dependencies and run-time dependencies are specified differently. In
your case, the postgresql jar is both: can you see if it's specified
as a run-time dependency as well?


Re: getNotifications

От
dmp
Дата:
dmp wrote:
>> I'm not sure, but I think I'm with Maciek's orginally response. I don't
>> think you're going to be able to cast a java.sql.Connection to PGConnection
>> then call one of the latter's methods.
>

Maciek Sakrejda wrote:
> You can, if that's the actual runtime type of the Connection object.
> You get a Connection back from DriverManager, so it has to work like
> that. If there's a pooler in the way things can get more hairy (i.e.,
> PGConnection can be wrapped in something that implements
> java.sql.Connection but is not a PGConnection, and that can end up as
> the only thing expose to the user), but k f was talking about a
> compile-time error.
>

Yep, your right. I took a closer look at the little piece of code that
was sent to the list its fine, but without the complete test code its
difficult to offer further help.

kfan:
> Maciek - I responded directly to your email and attached my test code.

As indicated please send info, test case code to list.

danap.



Re: getNotifications

От
kfan
Дата:
Ok. Sorry about that.  The source should be uploaded with this post if I did
it right as Maciek and danap have requested. Its a simple example I found
online.  I apologize if I failed to follow proper mail list etiquette!

I am using Eclipse.  I have specified the postgresql-9.0-801.jdbc4.jar in
the build path libraries. But I must be doing something wrong.  I just can't
figure out what that is!  Do I need something else?

Please feel free to mention the obvious!  As I mentioned I am new to this
and i may be missing something very simple.



--
View this message in context: http://postgresql.1045698.n5.nabble.com/getNotifications-tp5720228p5720292.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


Re: getNotifications

От
kfan
Дата:
Thank you for your help.  I've discovered my problem, I was also including
the postGIS jar.  Modifying the jar order, postgres jar first has solved the
problem.  Never expected a PGConnection class in there, or I'd have expected
it to have the same properties.



--
View this message in context: http://postgresql.1045698.n5.nabble.com/getNotifications-tp5720228p5720326.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.