You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "sdedic (via GitHub)" <gi...@apache.org> on 2023/03/06 07:11:22 UTC

[GitHub] [netbeans] sdedic commented on a diff in pull request #5560: Vscode ext should update default connection

sdedic commented on code in PR #5560:
URL: https://github.com/apache/netbeans/pull/5560#discussion_r1126016374


##########
ide/db/src/org/netbeans/modules/db/explorer/ConnectionList.java:
##########
@@ -273,32 +278,57 @@ public void setPreferredConnection(DatabaseConnection c) {
             }
             ref.clear();
             preferred = new PreferredRef(c);
+            lastPrefName = null;
         }
         dbPreferences.put(PREF_PREFERRED_CONNECTION_NAME, c.getName());
         fireListeners();
     }
     
     public DatabaseConnection getPreferredConnection(boolean selectFirst) {
-        DatabaseConnection p = preferred.get();
-        if (p != null) {
-            return p;
+        String fallback;
+        DatabaseConnection p;
+        synchronized (this) {
+            p = preferred.get();
+            if (p != null) {
+                return p;
+            }
+            fallback = this.lastPrefName;
         }
         String prefName = dbPreferences.get(PREF_PREFERRED_CONNECTION_NAME, null);
         DatabaseConnection[] conns = getConnections();
-        if (prefName == null) {
-            return conns.length == 0 || !selectFirst ? null : conns[0];
+        DatabaseConnection selected = findConnection(prefName, conns);
+        boolean prefChanged = false;
+        if (selected == null) {
+            if (conns.length > 0) {
+                // find the last one, or just anything.
+                selected = findConnection(fallback, conns);
+            }
+            if (selected != null) {
+                prefChanged = !Objects.equals(lastPrefName, selected.getName());
+                lastPrefName = selected.getName();
+            } else {
+                return null;
+            }
         }
+        synchronized (this) {
+            p = preferred.get();
+            if (p == null) {
+                preferred = new PreferredRef(selected);
+                p = selected;
+            }
+        }
+        if (prefChanged) {
+            fireListeners();
+        }
+        return p;
+    }
+    
+    private static DatabaseConnection findConnection(String name, DatabaseConnection[] conns) {
         for (int i = 0; i < conns.length; i++) {

Review Comment:
   I usually use for-loops in these situations ... this part was actually extracted from the older code that is obviously no excuse for not updating the style. I usually incorporate 'style issues' whenever some func/perf ("real") issue is found and a new commit has to be made.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists