You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/03/26 09:32:40 UTC

[GitHub] [netbeans] vieiro commented on a change in pull request #3868: SQL History loss: Reimplement atomatic saving by writing to tempfile and moving to target

vieiro commented on a change in pull request #3868:
URL: https://github.com/apache/netbeans/pull/3868#discussion_r835742869



##########
File path: ide/db.core/src/org/netbeans/modules/db/sql/history/SQLHistoryManager.java
##########
@@ -205,40 +209,34 @@ void removePropertyChangeListener(PropertyChangeListener listener) {
         @Override
         public void run() {
             try {
-                final FileObject targetFile = getHistoryRoot(true);
-                targetFile.getFileSystem().
-                        runAtomicAction(new FileSystem.AtomicAction() {
-                    @Override
-                    public void run() throws IOException {
-                        ;
-                        try (OutputStream os = targetFile.getOutputStream()) {
-                            XMLStreamWriter xsw = XMLOutputFactory
-                                    .newInstance()
-                                    .createXMLStreamWriter(os);
-                            
-                            xsw.writeStartDocument();
-                            xsw.writeCharacters(CONTENT_NEWLINE);
-                            xsw.writeStartElement(TAG_HISTORY);
-                            xsw.writeCharacters(CONTENT_NEWLINE);
-                            for(SQLHistoryEntry sqe: sqlHistory) {
-                                xsw.writeStartElement(TAG_SQL);
-                                xsw.writeAttribute(ATTR_DATE, sqe.getDateXMLVariant());
-                                xsw.writeAttribute(ATTR_URL, sqe.getUrl());
-                                xsw.writeCharacters(sqe.getSql());
-                                xsw.writeEndElement();
-                                xsw.writeCharacters(CONTENT_NEWLINE);
-                            }
-                            xsw.writeEndElement();
-                            xsw.flush();
-                            xsw.close();
-                        } catch (IOException | XMLStreamException ex) {
-                            LOGGER.log(Level.INFO, ex.getMessage(), ex);
-                        } finally {
-                            PROPERTY_CHANGE_SUPPORT.firePropertyChange(
-                                    PROP_SAVED, null, null);
-                        }
+                final FileObject targetFileObject = getHistoryRoot(true);
+                final Path targetFile = FileUtil.toFile(targetFileObject).toPath();
+                final Path tempfile = Files.createTempFile(targetFile.getParent(), SQL_HISTORY_BASE, SQL_HISTORY_EXT);
+                try ( OutputStream os = Files.newOutputStream(tempfile)) {
+                    XMLStreamWriter xsw = XMLOutputFactory
+                            .newInstance()
+                            .createXMLStreamWriter(os);
+
+                    xsw.writeStartDocument();
+                    xsw.writeCharacters(CONTENT_NEWLINE);
+                    xsw.writeStartElement(TAG_HISTORY);
+                    xsw.writeCharacters(CONTENT_NEWLINE);
+                    for (SQLHistoryEntry sqe : sqlHistory) {
+                        xsw.writeStartElement(TAG_SQL);
+                        xsw.writeAttribute(ATTR_DATE, sqe.getDateXMLVariant());
+                        xsw.writeAttribute(ATTR_URL, sqe.getUrl());
+                        xsw.writeCharacters(sqe.getSql());
+                        xsw.writeEndElement();
+                        xsw.writeCharacters(CONTENT_NEWLINE);
                     }
-                });
+                    xsw.writeEndElement();
+                    xsw.flush();
+                    xsw.close();
+                } catch (IOException | XMLStreamException ex) {
+                    LOGGER.log(Level.INFO, ex.getMessage(), ex);

Review comment:
       If we catch an exception here, are we sure we want to invoke `Files.move` (below) immediately after? 
   
   Or do we want to add a `return` statement after we log the exception so no files (with potential errors) are `Files.move`d?

##########
File path: ide/db.core/src/org/netbeans/modules/db/sql/history/SQLHistoryManager.java
##########
@@ -97,7 +101,7 @@ public int getListSize() {
         return NbPreferences.forModule(SQLHistoryPanel.class).getInt("OPT_SQL_STATEMENTS_SAVED_FOR_HISTORY", DEFAULT_SQL_STATEMENTS_SAVED_FOR_HISTORY);
     }
 
-    protected FileObject getHistoryRoot(boolean create) throws IOException {
+    private FileObject getHistoryRoot(boolean create) throws IOException {

Review comment:
       `FileUtil.createFolder` [accepts names with forward slashes](https://bits.netbeans.org/dev/javadoc/org-openide-filesystems/org/openide/filesystems/FileUtil.html#createFolder-org.openide.filesystems.FileObject-java.lang.String-) so we're good with that `"Databases/SQLHISTORY"` string.




-- 
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