File tree Expand file tree Collapse file tree
chainbase/src/main/java/org/tron Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -174,9 +174,14 @@ public Path getDbPath() {
174174 * reset database.
175175 */
176176 public void resetDb () {
177- closeDB ();
178- FileUtil .recursiveDelete (getDbPath ().toString ());
179- initDB ();
177+ resetDbLock .writeLock ().lock ();
178+ try {
179+ closeDB ();
180+ FileUtil .recursiveDelete (getDbPath ().toString ());
181+ initDB ();
182+ } finally {
183+ resetDbLock .writeLock ().unlock ();
184+ }
180185 }
181186
182187 @ Override
@@ -506,9 +511,17 @@ private DBIterator getDBIterator() {
506511 */
507512 @ Override
508513 public List <String > getStats () throws Exception {
509- String stat = database .getProperty ("leveldb.stats" );
510- String [] stats = stat .split ("\n " );
511- return Arrays .stream (stats ).skip (3 ).collect (Collectors .toList ());
514+ resetDbLock .readLock ().lock ();
515+ try {
516+ if (!isAlive ()) {
517+ return Collections .emptyList ();
518+ }
519+ String stat = database .getProperty ("leveldb.stats" );
520+ String [] stats = stat .split ("\n " );
521+ return Arrays .stream (stats ).skip (3 ).collect (Collectors .toList ());
522+ } finally {
523+ resetDbLock .readLock ().unlock ();
524+ }
512525 }
513526
514527 @ Override
Original file line number Diff line number Diff line change @@ -109,9 +109,14 @@ public void closeDB() {
109109
110110 @ Override
111111 public void resetDb () {
112- closeDB ();
113- FileUtil .recursiveDelete (getDbPath ().toString ());
114- initDB ();
112+ resetDbLock .writeLock ().lock ();
113+ try {
114+ closeDB ();
115+ FileUtil .recursiveDelete (getDbPath ().toString ());
116+ initDB ();
117+ } finally {
118+ resetDbLock .writeLock ().unlock ();
119+ }
115120 }
116121
117122 private boolean quitIfNotAlive () {
@@ -528,9 +533,17 @@ public RocksDbDataSourceImpl newInstance() {
528533 */
529534 @ Override
530535 public List <String > getStats () throws Exception {
531- String stat = database .getProperty ("rocksdb.levelstats" );
532- String [] stats = stat .split ("\n " );
533- return Arrays .stream (stats ).skip (2 ).collect (Collectors .toList ());
536+ resetDbLock .readLock ().lock ();
537+ try {
538+ if (!isAlive ()) {
539+ return Collections .emptyList ();
540+ }
541+ String stat = database .getProperty ("rocksdb.levelstats" );
542+ String [] stats = stat .split ("\n " );
543+ return Arrays .stream (stats ).skip (2 ).collect (Collectors .toList ());
544+ } finally {
545+ resetDbLock .readLock ().unlock ();
546+ }
534547 }
535548
536549 @ Override
Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ protected TronDatabase(String dbName) {
5959 }
6060
6161 @ PostConstruct
62- private void init () {
62+ protected void init () {
6363 dbStatService .register (dbSource );
6464 }
6565
Original file line number Diff line number Diff line change @@ -45,4 +45,9 @@ public void forEach(Consumer action) {
4545 public Spliterator spliterator () {
4646 return null ;
4747 }
48- }
48+
49+ @ Override
50+ protected void init () {
51+ }
52+
53+ }
You can’t perform that action at this time.
0 commit comments