This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 209720
Collapse All | Expand All

(-)db.core/src/org/netbeans/modules/db/sql/history/SQLHistoryManager.java (-7 / +59 lines)
Lines 52-65 Link Here
52
import javax.xml.bind.Unmarshaller;
52
import javax.xml.bind.Unmarshaller;
53
import org.netbeans.modules.db.sql.execute.ui.SQLHistoryPanel;
53
import org.netbeans.modules.db.sql.execute.ui.SQLHistoryPanel;
54
import org.openide.filesystems.FileObject;
54
import org.openide.filesystems.FileObject;
55
import org.openide.filesystems.FileSystem;
55
import org.openide.filesystems.FileUtil;
56
import org.openide.filesystems.FileUtil;
56
import org.openide.util.NbPreferences;
57
import org.openide.util.NbPreferences;
58
import org.openide.util.RequestProcessor;
57
59
58
/**
60
/**
59
 *
61
 *
60
 * @author John Baker
62
 * @author John Baker
61
 */
63
 */
62
public class SQLHistoryManager  {
64
public class SQLHistoryManager {
63
65
64
    JAXBContext context;
66
    JAXBContext context;
65
    private static final String SQL_HISTORY_DIRECTORY = "Databases/SQLHISTORY"; // NOI18N
67
    private static final String SQL_HISTORY_DIRECTORY = "Databases/SQLHISTORY"; // NOI18N
Lines 70-75 Link Here
70
    private static SQLHistoryManager _instance = null;    
72
    private static SQLHistoryManager _instance = null;    
71
    private static final Logger LOGGER = Logger.getLogger(SQLHistoryEntry.class.getName());
73
    private static final Logger LOGGER = Logger.getLogger(SQLHistoryEntry.class.getName());
72
    private SQLHistory sqlHistory;
74
    private SQLHistory sqlHistory;
75
    private static final RequestProcessor RP = new RequestProcessor(SQLHistoryManager.class.getName(), 1, false, false);
76
    private final RequestProcessor.Task SAVER = RP.create(new Saver());
77
    // Time between call to save and real save - usefull to accumulate before save
78
    private static final int SAVE_DELAY = 5 * 1000;
73
79
74
    protected SQLHistoryManager() {
80
    protected SQLHistoryManager() {
75
        ClassLoader orig = Thread.currentThread().getContextClassLoader();
81
        ClassLoader orig = Thread.currentThread().getContextClassLoader();
Lines 82-93 Link Here
82
        } finally {
88
        } finally {
83
            Thread.currentThread().setContextClassLoader(orig);
89
            Thread.currentThread().setContextClassLoader(orig);
84
        }
90
        }
91
        Runtime.getRuntime().addShutdownHook(new Thread() {
92
            @Override
93
            public void run() {
94
                // If a save is pending on shutdown, enforce immediate write
95
                if (SAVER.getDelay() > 0) {
96
                    SAVER.schedule(0);
97
                    SAVER.waitFinished();
85
    }
98
    }
99
            }
100
        });
101
    }
86
    
102
    
87
    public static SQLHistoryManager getInstance() {
103
    public static SQLHistoryManager getInstance() {
104
        synchronized (SQLHistoryManager.class) {
88
        if (_instance == null) {
105
        if (_instance == null) {
89
            _instance = new SQLHistoryManager();                    
106
            _instance = new SQLHistoryManager();                    
90
        } 
107
        } 
108
        }
91
        return _instance;
109
        return _instance;
92
    }
110
    }
93
111
Lines 105-111 Link Here
105
            FileObject historyRoot = historyRootDir.getFileObject(getHistoryFilename());
123
            FileObject historyRoot = historyRootDir.getFileObject(getHistoryFilename());
106
124
107
            if (historyRoot != null || create) {
125
            if (historyRoot != null || create) {
108
                if(historyRoot == null) {
126
                if (historyRoot == null) {
109
                    historyRoot = historyRootDir.createData(getHistoryFilename());
127
                    historyRoot = historyRootDir.createData(getHistoryFilename());
110
    }
128
    }
111
                result = historyRoot;
129
                result = historyRoot;
Lines 150-166 Link Here
150
    }
168
    }
151
    
169
    
152
    public void save() {
170
    public void save() {
171
        // On call to save schedule real saving, as save is a oftem calleed
172
        // method, this can bundle multiple saves into one write
173
        //
174
        // There is an potential for a dataloss in case of a forced shutdown
175
        // of the jvm, but this is considered acceptable (normal shutdown
176
        // is catered for by a shutdown hook)
177
        if (SAVER.getDelay() == 0) {
178
            SAVER.schedule(SAVE_DELAY);
179
        }
180
    }
181
182
    public SQLHistory getSQLHistory() {
183
        return sqlHistory;
184
    }
185
186
    private class Saver implements Runnable {
187
188
        @Override
189
        public void run() {
153
        try {
190
        try {
191
                final FileObject targetFile = getHistoryRoot(true);
192
                targetFile.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
193
194
                    @Override
195
                    public void run() throws IOException {
196
                        OutputStream os = null;
197
                        try {
154
            Marshaller marshaller = context.createMarshaller();
198
            Marshaller marshaller = context.createMarshaller();
155
            OutputStream os = getHistoryRoot(true).getOutputStream();
199
                            os = targetFile.getOutputStream();
156
            marshaller.marshal(sqlHistory, os);
200
            marshaller.marshal(sqlHistory, os);
157
            os.close();
158
        } catch (Exception ex) {
201
        } catch (Exception ex) {
159
            LOGGER.log(Level.INFO, ex.getMessage());
202
            LOGGER.log(Level.INFO, ex.getMessage());
203
                        } finally {
204
                            try {
205
                                if (os != null) {
206
                                    os.close();
160
            }
207
            }
208
                            } catch (IOException ex) {
161
        }    
209
        }    
162
163
    public SQLHistory getSQLHistory() {
164
        return sqlHistory;
165
    }
210
    }
166
}
211
}
212
                });
213
            } catch (IOException ex) {
214
                LOGGER.log(Level.INFO, ex.getMessage());
215
            }
216
        }
217
    }
218
}
(-)db.core/test/unit/src/org/netbeans/modules/db/sql/history/SQLHistoryPersistenceManagerTest.java (-6 / +12 lines)
Lines 63-74 Link Here
63
        super(testName);
63
        super(testName);
64
    }
64
    }
65
65
66
    /** Called before every test case. */
67
    @Override
68
    public void setUp() {
69
        System.out.println("########  " + getName() + "  #######");
70
    }
71
72
    /** Called after every test case. */
66
    /** Called after every test case. */
73
    @Override
67
    @Override
74
    public void tearDown() throws IOException {
68
    public void tearDown() throws IOException {
Lines 94-102 Link Here
94
            }
88
            }
95
            
89
            
96
        };
90
        };
91
        // History does not yet exists as file
92
        assert (testableManager.getHistoryRoot(false) == null);
97
        testableManager.getSQLHistory().add(new SQLHistoryEntry("jdbc:// mysql", "select * from TRAVEL.PERSON", Calendar.getInstance().getTime()));
93
        testableManager.getSQLHistory().add(new SQLHistoryEntry("jdbc:// mysql", "select * from TRAVEL.PERSON", Calendar.getInstance().getTime()));
94
        // History does not yet exists as file
95
        testableManager.save();
96
        assert (testableManager.getHistoryRoot(false) == null);
98
        testableManager.getSQLHistory().add(new SQLHistoryEntry("jdbc:// oracle", "select * from PERSON", Calendar.getInstance().getTime()));
97
        testableManager.getSQLHistory().add(new SQLHistoryEntry("jdbc:// oracle", "select * from PERSON", Calendar.getInstance().getTime()));
98
        // History does not yet exists as file
99
        testableManager.save();
99
        testableManager.save();
100
        assert (testableManager.getHistoryRoot(false) == null);
101
        // Enforce writing of history
102
        Thread.sleep(6 * 1000);
103
        // History file need to exist now!
104
        assert (testableManager.getHistoryRoot(false) != null);
105
        assert (testableManager.getHistoryRoot(false).isData());
100
    }
106
    }
101
107
102
    /** Tests parsing of date format. */
108
    /** Tests parsing of date format. */

Return to bug 209720