19 package org.sleuthkit.autopsy.centralrepository.datamodel;
21 import java.sql.Connection;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24 import java.util.concurrent.TimeUnit;
25 import java.util.logging.Level;
26 import org.apache.commons.dbcp2.BasicDataSource;
35 final class PostgresEamDb
extends AbstractSqlEamDb {
37 private final static Logger LOGGER = Logger.
getLogger(PostgresEamDb.class.getName());
39 private final static String CONFLICT_CLAUSE =
"ON CONFLICT DO NOTHING";
41 private static PostgresEamDb instance;
43 private static final int CONN_POOL_SIZE = 10;
44 private BasicDataSource connectionPool = null;
46 private final PostgresEamDbSettings dbSettings;
55 public synchronized static PostgresEamDb getInstance() throws EamDbException {
56 if (instance == null) {
57 instance =
new PostgresEamDb();
68 private PostgresEamDb() throws EamDbException {
69 dbSettings =
new PostgresEamDbSettings();
70 bulkArtifactsThreshold = dbSettings.getBulkThreshold();
74 public void shutdownConnections() throws EamDbException {
77 if(connectionPool != null){
78 connectionPool.close();
79 connectionPool = null;
82 }
catch (SQLException ex) {
83 throw new EamDbException(
"Failed to close existing database connections.", ex);
88 public void updateSettings() {
90 dbSettings.loadSettings();
91 bulkArtifactsThreshold = dbSettings.getBulkThreshold();
96 public void saveSettings() {
98 dbSettings.saveSettings();
103 public void reset() throws EamDbException {
104 Connection conn = connect();
107 Statement dropContent = conn.createStatement();
108 dropContent.executeUpdate(
"TRUNCATE TABLE organizations RESTART IDENTITY CASCADE");
109 dropContent.executeUpdate(
"TRUNCATE TABLE cases RESTART IDENTITY CASCADE");
110 dropContent.executeUpdate(
"TRUNCATE TABLE data_sources RESTART IDENTITY CASCADE");
111 dropContent.executeUpdate(
"TRUNCATE TABLE reference_sets RESTART IDENTITY CASCADE");
112 dropContent.executeUpdate(
"TRUNCATE TABLE correlation_types RESTART IDENTITY CASCADE");
113 dropContent.executeUpdate(
"TRUNCATE TABLE db_info RESTART IDENTITY CASCADE");
115 String instancesTemplate =
"TRUNCATE TABLE %s_instances RESTART IDENTITY CASCADE";
116 String referencesTemplate =
"TRUNCATE TABLE reference_%s RESTART IDENTITY CASCADE";
117 for (CorrelationAttribute.Type type : defaultCorrelationTypes) {
118 dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
120 if (type.getId() == CorrelationAttribute.FILES_TYPE_ID) {
121 dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
124 }
catch (SQLException ex) {
125 LOGGER.log(Level.WARNING,
"Failed to reset database.", ex);
127 EamDbUtil.closeConnection(conn);
130 dbSettings.insertDefaultDatabaseContent();
137 private void setupConnectionPool() throws EamDbException {
138 connectionPool =
new BasicDataSource();
139 connectionPool.setUsername(dbSettings.getUserName());
140 connectionPool.setPassword(dbSettings.getPassword());
141 connectionPool.setDriverClassName(dbSettings.getDriver());
143 StringBuilder connectionURL =
new StringBuilder();
144 connectionURL.append(dbSettings.getJDBCBaseURI());
145 connectionURL.append(dbSettings.getHost());
146 connectionURL.append(
":");
147 connectionURL.append(dbSettings.getPort());
148 connectionURL.append(
"/");
149 connectionURL.append(dbSettings.getDbName());
151 connectionPool.setUrl(connectionURL.toString());
152 connectionPool.setUsername(dbSettings.getUserName());
153 connectionPool.setPassword(dbSettings.getPassword());
156 connectionPool.setInitialSize(5);
157 connectionPool.setMaxIdle(CONN_POOL_SIZE);
158 connectionPool.setValidationQuery(dbSettings.getValidationQuery());
169 protected Connection connect() throws EamDbException {
170 synchronized (
this) {
171 if (!EamDb.isEnabled()) {
172 throw new EamDbException(
"Central Repository module is not enabled");
175 if (connectionPool == null) {
176 setupConnectionPool();
181 return connectionPool.getConnection();
182 }
catch (SQLException ex) {
183 throw new EamDbException(
"Error getting connection from connection pool.", ex);
188 protected String getConflictClause() {
189 return CONFLICT_CLAUSE;
201 public CoordinationService.Lock getExclusiveMultiUserDbLock() throws EamDbException{
204 if( ! UserPreferences.getIsMultiUserModeEnabled()){
208 String databaseNodeName = dbSettings.getHost() +
"_" + dbSettings.getDbName();
209 CoordinationService.Lock lock = CoordinationService.getInstance().tryGetExclusiveLock(CoordinationService.CategoryNode.CENTRAL_REPO, databaseNodeName, 5, TimeUnit.MINUTES);
214 throw new EamDbException(
"Error acquiring database lock");
215 }
catch (InterruptedException ex){
216 throw new EamDbException(
"Error acquiring database lock");
217 }
catch (CoordinationService.CoordinationServiceException ex) {
synchronized static Logger getLogger(String name)