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 1 commit
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
73 changes: 40 additions & 33 deletions src/main/java/org/sqlite/SQLiteJDBCLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,46 @@ public class SQLiteJDBCLoader {
* @return True if SQLite native library is successfully loaded; false otherwise.
*/
public static boolean initialize() throws Exception {
loadSQLiteNativeLibrary();
return extracted;
// only cleanup before first extract
if(!extracted) {
Copy link
Owner

Choose a reason for hiding this comment

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

weird indentation

cleanup();
}
loadSQLiteNativeLibrary();
return extracted;
}


/**
* Deleted old nativ libraries e.g. on Windows this are not removed on VM-Exit (bug #80)
Copy link
Owner

Choose a reason for hiding this comment

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

typo: native

*/
static void cleanup(){

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

final String ver = org.sqlite.SQLiteJDBCLoader.getVersion();
Copy link
Owner

Choose a reason for hiding this comment

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

unnecessary final and remove org.sqlite.SQLiteJDBCLoder

String tempFolder = new java.io.File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
Copy link
Owner

Choose a reason for hiding this comment

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

Just import java.io.File

java.io.File dir = new java.io.File(tempFolder);
java.io.File [] files = dir.listFiles(new java.io.FilenameFilter() {
@Override
public boolean accept(java.io.File dir, String name) {
return name.startsWith("sqlite-" + ver);
Copy link
Owner

Choose a reason for hiding this comment

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

Use private variable e.g., private final String prefix ="sqlite-" + ver to minimize String generation cost.

}
});

for (java.io.File tempFile : files) {
Copy link
Owner

Choose a reason for hiding this comment

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

listFiles can return null so this might throw NullPointerException

if(tempFile.getName().indexOf(".lck") > 0) { continue; }
java.io.File lckFile = new java.io.File(tempFile.getName() + ".lck");
if(!lckFile.exists()) {
try{
tempFile.delete();
}
catch(SecurityException ex){

Copy link
Owner

Choose a reason for hiding this comment

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

Swallowing exception is a bad habit. At least we should show the error message here.

}

}
}


}

/**
* @return True if the SQLite JDBC driver is set to pure Java mode; false otherwise.
Expand Down Expand Up @@ -181,37 +218,7 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
extractedLibFile.deleteOnExit();
extractedLckFile.deleteOnExit();

// Add a Shutdown-Hook to remove other unused binaries (dll in temp without lck-file)
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
System.gc();
final String ver = org.sqlite.SQLiteJDBCLoader.getVersion();

String tempFolder = new java.io.File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
java.io.File dir = new java.io.File(tempFolder);
java.io.File [] files = dir.listFiles(new java.io.FilenameFilter() {

@Override
public boolean accept(java.io.File dir, String name)
{
return name.startsWith("sqlite-" + ver);
}
});

for (java.io.File tempFile : files)
{
if(tempFile.getName().indexOf(".lck") > 0) { continue; }
java.io.File lckFile = new java.io.File(tempFile.getName() + ".lck");
if(!lckFile.exists()){
tempFile.deleteOnExit();
}
}

}
});

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