parent
b88bdb37c9
commit
76e8abd219
@ -0,0 +1,60 @@ |
|||||||
|
package org.airsonic.player.dao; |
||||||
|
|
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.springframework.jdbc.core.JdbcTemplate; |
||||||
|
import org.springframework.jdbc.datasource.DataSourceUtils; |
||||||
|
|
||||||
|
import javax.annotation.PreDestroy; |
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import java.sql.Connection; |
||||||
|
import java.sql.SQLException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Special Dao Helper with additional features for managing the legacy embedded HSQL database. |
||||||
|
*/ |
||||||
|
public class LegacyHsqlDaoHelper extends GenericDaoHelper { |
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(LegacyHsqlDaoHelper.class); |
||||||
|
|
||||||
|
public LegacyHsqlDaoHelper(DataSource dataSource) { |
||||||
|
super(dataSource); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean checkpoint() { |
||||||
|
// HSQLDB (at least version 1) does not handle automatic checkpoints very well by default.
|
||||||
|
// This makes sure the temporary log is actually written to more persistent storage.
|
||||||
|
LOG.info("Database checkpoint in progress..."); |
||||||
|
getJdbcTemplate().execute("CHECKPOINT DEFRAG"); |
||||||
|
LOG.info("Database checkpoint complete."); |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@PreDestroy |
||||||
|
public void onDestroy() { |
||||||
|
Connection conn = null; |
||||||
|
try { |
||||||
|
// Properly shutdown the embedded HSQLDB database.
|
||||||
|
LOG.info("Database shutdown in progress..."); |
||||||
|
JdbcTemplate jdbcTemplate = getJdbcTemplate(); |
||||||
|
conn = DataSourceUtils.getConnection(jdbcTemplate.getDataSource()); |
||||||
|
conn.setAutoCommit(true); |
||||||
|
jdbcTemplate.execute("SHUTDOWN"); |
||||||
|
LOG.info("Database shutdown complete."); |
||||||
|
|
||||||
|
} catch (SQLException e) { |
||||||
|
LOG.error("Database shutdown failed: " + e); |
||||||
|
e.printStackTrace(); |
||||||
|
|
||||||
|
} finally { |
||||||
|
try { |
||||||
|
if(conn != null) |
||||||
|
conn.close(); |
||||||
|
} catch(Exception ex) { |
||||||
|
ex.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,55 +0,0 @@ |
|||||||
package org.airsonic.player.spring; |
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.context.ApplicationContext; |
|
||||||
import org.springframework.jdbc.core.JdbcTemplate; |
|
||||||
import org.springframework.jdbc.datasource.DataSourceUtils; |
|
||||||
|
|
||||||
import javax.annotation.PreDestroy; |
|
||||||
import javax.sql.DataSource; |
|
||||||
|
|
||||||
import java.sql.Connection; |
|
||||||
import java.sql.DatabaseMetaData; |
|
||||||
import java.sql.SQLException; |
|
||||||
|
|
||||||
public class TerminateBean { |
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(TerminateBean.class); |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private ApplicationContext context; |
|
||||||
|
|
||||||
@PreDestroy |
|
||||||
public void onDestroy() { |
|
||||||
Connection conn = null; |
|
||||||
try { |
|
||||||
// Connect to the database and retrieve the db name and version
|
|
||||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.context.getBean(DataSource.class)); |
|
||||||
conn = DataSourceUtils.getConnection(jdbcTemplate.getDataSource()); |
|
||||||
DatabaseMetaData meta = conn.getMetaData(); |
|
||||||
String productName = meta.getDatabaseProductName(); |
|
||||||
int productVersion = meta.getDatabaseMajorVersion(); |
|
||||||
|
|
||||||
// Properly shutdown HSQLDB databases
|
|
||||||
if (productName.equals("HSQL Database Engine") && (productVersion == 1 || productVersion == 2)) { |
|
||||||
LOG.info("Database shutdown in progress..."); |
|
||||||
conn.setAutoCommit(true); |
|
||||||
jdbcTemplate.execute("SHUTDOWN"); |
|
||||||
LOG.info("Database shutdown complete."); |
|
||||||
} |
|
||||||
|
|
||||||
} catch (SQLException e) { |
|
||||||
e.printStackTrace(); |
|
||||||
|
|
||||||
} finally { |
|
||||||
try { |
|
||||||
if(conn != null) |
|
||||||
conn.close(); |
|
||||||
} catch(Exception ex) { |
|
||||||
ex.printStackTrace(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue