sql >> Database teknologi >  >> RDS >> PostgreSQL

LYT/GIV MEDDELELSE pgconnection går ned java?

Notifikationslytterne vedligeholdes internt af det pågældende bibliotek som svage referencer, hvilket betyder, at du skal have en hård reference eksternt, så de ikke bliver indsamlet affald. Tjek BasicContext klasse linjerne 642 - 655:

public void addNotificationListener(String name, String channelNameFilter, NotificationListener listener) {

    name = nullToEmpty(name);
    channelNameFilter = channelNameFilter != null ? channelNameFilter : ".*";

    Pattern channelNameFilterPattern = Pattern.compile(channelNameFilter);

    NotificationKey key = new NotificationKey(name, channelNameFilterPattern);

    synchronized (notificationListeners) {
      notificationListeners.put(key, new WeakReference<NotificationListener>(listener));
    }

}

Hvis GC'en opfanger din lytter, vil opkald for at "komme" på den svage reference returnere null og vil ikke udløses som set fra linje 690 - 710

  @Override
  public synchronized void reportNotification(int processId, String channelName, String payload) {

    Iterator<Map.Entry<NotificationKey, WeakReference<NotificationListener>>> iter = notificationListeners.entrySet().iterator();
    while (iter.hasNext()) {

      Map.Entry<NotificationKey, WeakReference<NotificationListener>> entry = iter.next();

      NotificationListener listener = entry.getValue().get();
      if (listener == null) {

        iter.remove();
      }
      else if (entry.getKey().channelNameFilter.matcher(channelName).matches()) {

        listener.notification(processId, channelName, payload);
      }

    }

}

For at løse dette skal du tilføje dine notifikationslyttere som sådan:

/// Do not let this reference go out of scope!
    PGNotificationListener listener = new PGNotificationListener() {

    @Override
    public void notification(int processId, String channelName, String payload) {
        // interesting code
    };
};
    pgConnection.addNotificationListener(listener);

En ret mærkelig use-case for svage referencer efter min mening...




  1. Brug af korreleret underforespørgsel

  2. JDBC konverterer tidsstempel til NULL (problem med zeroDateTimeBehavior)

  3. Databasedesign med Vertabelo

  4. Hvad er et indeks?