Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Lck-File that is correctly deleted on JVM-Exit. #173

Merged
merged 4 commits into from
Oct 31, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions src/main/java/org/sqlite/SQLiteJDBCLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -55,19 +56,55 @@
public class SQLiteJDBCLoader {

private static boolean extracted = false;
private static String searchPattern = "sqlite-" + getVersion();

/**
* Loads SQLite native JDBC library.
*
* @return True if SQLite native library is successfully loaded; false otherwise.
* @return True if SQLite native library is successfully loaded; false
* otherwise.
*/
public static boolean initialize() throws Exception {
// only cleanup before first extract
if(!extracted) {
cleanup();
}
loadSQLiteNativeLibrary();
return extracted;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line

/**
* @return True if the SQLite JDBC driver is set to pure Java mode; false otherwise.
* Deleted old native libraries e.g. on Windows the DLL file is not removed
* on VM-Exit (bug #80)
*/
static void cleanup() {
String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
File dir = new File(tempFolder);
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(searchPattern);
}
});
if(files != null) {
for (File tempFile : files) {
if(tempFile.getName().indexOf(".lck") > 0) {
continue;
}
File lckFile = new File(tempFile.getName() + ".lck");
if(!lckFile.exists()) {
try {
tempFile.delete();
} catch (SecurityException e) {
System.err.println("Fail deleting old native lib" + e.getMessage());
}
}
}
}
}

/**
* @return True if the SQLite JDBC driver is set to pure Java mode; false
* otherwise.
* @deprecated Pure Java no longer supported
*/
static boolean getPureJavaFlag() {
Expand Down Expand Up @@ -157,7 +194,11 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
// when multiple JVMs with different architectures running at the same time
String uuid = UUID.randomUUID().toString();
String extractedLibFileName = String.format("sqlite-%s-%s-%s", getVersion(), uuid, libraryFileName);
String extractedLckFileName = extractedLibFileName + ".lck";

File extractedLibFile = new File(targetFolder, extractedLibFileName);
File extractedLckFile = new File(targetFolder, extractedLckFileName);


try {
// Extract a native library file into the target directory
Expand All @@ -170,9 +211,14 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
writer.write(buffer, 0, bytesRead);
}
} finally {
if (!extractedLckFile.exists()) {
new FileOutputStream(extractedLckFile).close();
}
// Delete the extracted lib file on JVM exit.
extractedLibFile.deleteOnExit();

extractedLckFile.deleteOnExit();


if(writer != null) {
writer.close();
}
Expand Down