Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Manually merged pull request 'h2 database update to 1.3.168, improved…

… handling of database exceptions' as it wasn't applicable without modifications (closes #81)

Updated changelog
  • Loading branch information...
commit 3b8b0df40a6e1a53174a017b810a97d3a1bb519c 2 parents 346e9a6 + 4fb2928
Philippe taconaut authored
1  CHANGELOG
View
@@ -4,6 +4,7 @@ Changelog:
1.71.0 - 2012-??-??
Updated MediaInfo to 0.7.60
+ Updated h2 database to 1.3.168 (thanks, valib!)
Improve handling of initialization errors
Added support for Samsung SMT-G7400 (UPC Horizon)
2  pom.xml
View
@@ -221,7 +221,7 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
- <version>1.1.105</version>
+ <version>1.3.168</version>
</dependency>
<dependency>
136 src/main/java/net/pms/dlna/DLNAMediaDatabase.java
View
@@ -23,18 +23,26 @@
import net.pms.PMS;
import net.pms.configuration.FormatConfiguration;
import net.pms.formats.v2.SubtitleType;
+
+import org.h2.engine.Constants;
import org.h2.jdbcx.JdbcConnectionPool;
import org.h2.jdbcx.JdbcDataSource;
+import org.h2.store.fs.FileUtils;
import org.h2.tools.DeleteDbFiles;
import org.h2.tools.RunScript;
import org.h2.tools.Script;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.awt.Component;
import java.io.File;
import java.sql.*;
import java.util.ArrayList;
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
+import javax.swing.SwingUtilities;
+
import static org.apache.commons.lang.StringUtils.*;
/**
@@ -44,12 +52,14 @@
* later.
*/
public class DLNAMediaDatabase implements Runnable {
- private static final Logger logger = LoggerFactory.getLogger(DLNAMediaDatabase.class);
+ private static final Logger LOGGER = LoggerFactory.getLogger(DLNAMediaDatabase.class);
private String url;
- private String dir;
+ private String dbDir;
+ private String dbName;
public static final String NONAME = "###";
private Thread scanner;
private JdbcConnectionPool cp;
+ private int dbCount;
// Database column sizes
private final int SIZE_CODECV = 32;
@@ -69,7 +79,8 @@
private final int SIZE_GENRE = 64;
public DLNAMediaDatabase(String name) {
- dir = "database";
+ String dir = "database";
+ dbName = name;
File fileDir = new File(dir);
boolean defaultLocation = fileDir.mkdir() || fileDir.exists();
if (defaultLocation) {
@@ -87,18 +98,19 @@ public DLNAMediaDatabase(String name) {
}
if (Platform.isWindows() && !defaultLocation) {
String profileDir = PMS.getConfiguration().getProfileDirectory();
- url = String.format("jdbc:h2:%s\\%s/%s", profileDir, dir, name);
+ url = String.format("jdbc:h2:%s\\%s/%s", profileDir, dir, dbName);
fileDir = new File(profileDir, dir);
} else {
- url = "jdbc:h2:" + dir + "/" + name;
+ url = Constants.START_URL + dir + "/" + dbName;
}
- logger.debug("Using database URL: " + url);
- logger.info("Using database located at: " + fileDir.getAbsolutePath());
+ dbDir = fileDir.getAbsolutePath();
+ LOGGER.debug("Using database URL: " + url);
+ LOGGER.info("Using database located at: " + dbDir);
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
- logger.error(null, e);
+ LOGGER.error(null, e);
}
JdbcDataSource ds = new JdbcDataSource();
@@ -113,18 +125,44 @@ private Connection getConnection() throws SQLException {
}
public void init(boolean force) {
- int count = -1;
+ dbCount = -1;
String version = null;
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
+
+ // Check whether the database is not severely damaged, corrupted or wrong version
+ boolean force_delete = false;
+ try {
+ conn = getConnection();
+ } catch (SQLException se) { // Connection can't be established, so delete the database
+ force_delete = true;
+ } finally {
+ close(conn);
+ if (FileUtils.exists(dbDir + File.separator + dbName + ".data.db") || force_delete){
+ FileUtils.deleteRecursive(dbDir, true);
+ if (!FileUtils.exists(dbDir)){
+ LOGGER.debug("The cache has been deleted because it was corrupt or had the wrong version");
+ } else {
+ if(!java.awt.GraphicsEnvironment.isHeadless()) {
+ JOptionPane.showMessageDialog(
+ (JFrame) (SwingUtilities.getWindowAncestor((Component) PMS.get().getFrame())),
+ String.format(Messages.getString("DLNAMediaDatabase.5"), dbDir),
+ "PS3 Media Server error",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ LOGGER.debug("Damaged cache can't be deleted. Stop the program and delete the folder \"" + dbDir + "\" manually");
+ }
+ }
+ }
+
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT count(*) FROM FILES");
if (rs.next()) {
- count = rs.getInt(1);
+ dbCount = rs.getInt(1);
}
rs.close();
stmt.close();
@@ -135,22 +173,17 @@ public void init(boolean force) {
version = rs.getString(1);
}
} catch (SQLException se) {
- logger.debug("Database not created or corrupted");
+ if (se.getErrorCode() != 42102) { // Don't log exception "Table "FILES" not found" which will be corrected in following step
+ LOGGER.error(null, se);
+ }
} finally {
close(rs);
close(stmt);
close(conn);
}
boolean force_reinit = !PMS.getVersion().equals(version); // here we can force a deletion for a specific version
- if (force || count == -1 || force_reinit) {
- logger.debug("Database will be (re)initialized");
-// if (force_reinit) {
-// JOptionPane.showMessageDialog(
-// (JFrame) (SwingUtilities.getWindowAncestor((Component) PMS.get().getFrame())),
-// Messages.getString("DLNAMediaDatabase.0"),
-// "Information",
-// JOptionPane.INFORMATION_MESSAGE);
-// }
+ if (force || dbCount == -1 || force_reinit) {
+ LOGGER.debug("Database will be (re)initialized");
try {
conn = getConnection();
executeUpdate(conn, "DROP TABLE FILES");
@@ -159,7 +192,9 @@ public void init(boolean force) {
executeUpdate(conn, "DROP TABLE AUDIOTRACKS");
executeUpdate(conn, "DROP TABLE SUBTRACKS");
} catch (SQLException se) {
- logger.debug("Caught exception", se);
+ if (se.getErrorCode() != 42102) { // Don't log exception "Table "FILES" not found" which will be corrected in following step
+ LOGGER.error(null, se);
+ }
}
try {
StringBuilder sb = new StringBuilder();
@@ -237,15 +272,15 @@ public void init(boolean force) {
+ (i + 2) + " );");
}
- logger.debug("Database initialized");
+ LOGGER.debug("Database initialized");
} catch (SQLException se) {
- logger.info("Error in table creation: " + se.getMessage());
+ LOGGER.info("Error in table creation: " + se.getMessage());
} finally {
close(conn);
}
} else {
- logger.debug("Database file count: " + count);
- logger.debug("Database version: " + version);
+ LOGGER.debug("Database file count: " + dbCount);
+ LOGGER.debug("Database version: " + version);
}
}
@@ -272,7 +307,7 @@ public boolean isDataExists(String name, long modified) {
found = true;
}
} catch (SQLException se) {
- logger.error(null, se);
+ LOGGER.error(null, se);
return false;
} finally {
close(rs);
@@ -360,7 +395,7 @@ public boolean isDataExists(String name, long modified) {
list.add(media);
}
} catch (SQLException se) {
- logger.error(null, se);
+ LOGGER.error(null, se);
return null;
} finally {
close(rs);
@@ -484,10 +519,10 @@ public synchronized void insertData(String name, long modified, int type, DLNAMe
close(insert);
}
} catch (SQLException se) {
- if (se.getMessage().contains("[23001")) {
- logger.debug("Duplicate key while inserting this entry: " + name + " into the database: " + se.getMessage());
+ if (se.getErrorCode() == 23001) {
+ LOGGER.debug("Duplicate key while inserting this entry: " + name + " into the database: " + se.getMessage());
} else {
- logger.error(null, se);
+ LOGGER.error(null, se);
}
} finally {
close(ps);
@@ -510,11 +545,10 @@ public synchronized void updateThumbnail(String name, long modified, int type, D
}
ps.executeUpdate();
} catch (SQLException se) {
- if (se.getMessage().contains("[23001")) {
- logger.debug("Duplicate key while inserting this entry: " + name + " into the database: " + se.getMessage());
- } else {
- logger.error(null, se);
- }
+ if (se.getErrorCode() == 23001) {
+ LOGGER.debug("Duplicate key while inserting this entry: " + name + " into the database: " + se.getMessage());
+ } else
+ LOGGER.error(null, se);;
} finally {
close(ps);
close(conn);
@@ -541,7 +575,7 @@ public synchronized void updateThumbnail(String name, long modified, int type, D
}
}
} catch (SQLException se) {
- logger.error(null, se);
+ LOGGER.error(null, se);
return null;
} finally {
close(rs);
@@ -560,10 +594,10 @@ public void cleanup() {
conn = getConnection();
ps = conn.prepareStatement("SELECT COUNT(*) FROM FILES");
rs = ps.executeQuery();
- int count = 0;
-
+ dbCount = 0;
+
if (rs.next()) {
- count = rs.getInt(1);
+ dbCount = rs.getInt(1);
}
rs.close();
@@ -572,7 +606,7 @@ public void cleanup() {
int i = 0;
int oldpercent = 0;
- if (count > 0) {
+ if (dbCount > 0) {
ps = conn.prepareStatement("SELECT FILENAME, MODIFIED, ID FROM FILES", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
rs = ps.executeQuery();
while (rs.next()) {
@@ -583,7 +617,7 @@ public void cleanup() {
rs.deleteRow();
}
i++;
- int newpercent = i * 100 / count;
+ int newpercent = i * 100 / dbCount;
if (newpercent > oldpercent) {
PMS.get().getFrame().setStatusLine(Messages.getString("DLNAMediaDatabase.2") + newpercent + "%");
oldpercent = newpercent;
@@ -591,7 +625,7 @@ public void cleanup() {
}
}
} catch (SQLException se) {
- logger.error(null, se);
+ LOGGER.error(null, se);
} finally {
close(rs);
close(ps);
@@ -617,7 +651,7 @@ public void cleanup() {
}
}
} catch (SQLException se) {
- logger.error(null, se);
+ LOGGER.error(null, se);
return null;
} finally {
close(rs);
@@ -633,7 +667,7 @@ private void close(ResultSet rs) {
rs.close();
}
} catch (SQLException e) {
- logger.error("error during closing:" + e.getMessage(), e);
+ LOGGER.error("error during closing:" + e.getMessage(), e);
}
}
@@ -643,7 +677,7 @@ private void close(Statement ps) {
ps.close();
}
} catch (SQLException e) {
- logger.error("error during closing:" + e.getMessage(), e);
+ LOGGER.error("error during closing:" + e.getMessage(), e);
}
}
@@ -653,7 +687,7 @@ private void close(Connection conn) {
conn.close();
}
} catch (SQLException e) {
- logger.error("error during closing:" + e.getMessage(), e);
+ LOGGER.error("error during closing:" + e.getMessage(), e);
}
}
@@ -666,7 +700,7 @@ public synchronized void scanLibrary() {
scanner = new Thread(this, "Library Scanner");
scanner.start();
} else if (scanner.isAlive()) {
- logger.info("Scanner is already running !");
+ LOGGER.info("Scanner is already running !");
} else {
scanner = new Thread(this, "Library Scanner");
scanner.start();
@@ -684,15 +718,15 @@ public void run() {
}
public void compact() {
- logger.info("Compacting database...");
+ LOGGER.info("Compacting database...");
PMS.get().getFrame().setStatusLine(Messages.getString("DLNAMediaDatabase.3"));
String filename = "database/backup.sql";
try {
Script.execute(url, "sa", "", filename);
- DeleteDbFiles.execute(dir, "medias", true); // TODO: rename "medias" -> "cache"
+ DeleteDbFiles.execute(dbDir, "medias", true); // TODO: rename "medias" -> "cache"
RunScript.execute(url, "sa", "", filename, null, false);
- } catch (Exception s) {
- logger.error("Error in compacting database: ", s);
+ } catch (SQLException se) {
+ LOGGER.error("Error in compacting database: ", se);
} finally {
File testsql = new File(filename);
if (testsql.exists() && !testsql.delete()) {
1  src/main/java/net/pms/messages.properties
View
@@ -3,6 +3,7 @@ DLNAMediaDatabase.1=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
DLNAMediaDatabase.2=Cleanup database...
DLNAMediaDatabase.3=Compacting database...
DLNAMediaDatabase.4=Scanning Folder:
+DLNAMediaDatabase.5=Damaged cache has to be deleted but the program couldn't do it.\nStop the program and delete the folder %s manually.
FFMpegDVRMSRemux.0=Alternative FFmpeg Path:
FFMpegDVRMSRemux.1=Settings for DVR-MS remuxing
FFMpegVideo.0=Encoder settings for AviSynth/FFmpeg engine only (Prefer AviSynth/MEncoder)
Please sign in to comment.
Something went wrong with that request. Please try again.