[Major] Refactored PosgreSQL persistence for easier extension
This commit is contained in:
parent
4e7b2dde0e
commit
c321712150
|
@ -119,6 +119,10 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
|||
this.txResult.setState(TransactionState.OPEN);
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return this.action;
|
||||
}
|
||||
|
||||
public TransactionResult getTxResult() {
|
||||
return this.txResult;
|
||||
}
|
||||
|
|
|
@ -86,6 +86,13 @@ import li.strolch.service.api.Command;
|
|||
*/
|
||||
public interface StrolchTransaction extends AutoCloseable {
|
||||
|
||||
/**
|
||||
* Returns the action for which this transaction was opened
|
||||
*
|
||||
* @return the action for which this transaction was opened
|
||||
*/
|
||||
public String getAction();
|
||||
|
||||
/**
|
||||
* Returns the name of the {@link StrolchRealm} for which this transaction was opened
|
||||
*
|
||||
|
|
|
@ -21,10 +21,7 @@ import javax.xml.parsers.SAXParserFactory;
|
|||
import javax.xml.transform.sax.SAXResult;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLXML;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.*;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Calendar;
|
||||
|
||||
|
@ -35,6 +32,7 @@ import li.strolch.model.xml.StrolchElementToSaxVisitor;
|
|||
import li.strolch.model.xml.XmlModelSaxReader;
|
||||
import li.strolch.persistence.api.ActivityDao;
|
||||
import li.strolch.persistence.api.StrolchPersistenceException;
|
||||
import li.strolch.persistence.api.TransactionResult;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
|
@ -43,8 +41,8 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
|
|||
|
||||
public static final String ACTIVITIES = "activities";
|
||||
|
||||
protected PostgreSqlActivityDao(PostgreSqlStrolchTransaction tx) {
|
||||
super(tx);
|
||||
public PostgreSqlActivityDao(Connection connection, TransactionResult txResult, boolean versioningEnabled) {
|
||||
super(connection, txResult, versioningEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -80,7 +78,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
|
|||
|
||||
protected SQLXML createSqlXml(Activity activity, PreparedStatement preparedStatement)
|
||||
throws SQLException, SAXException {
|
||||
SQLXML sqlxml = tx().getConnection().createSQLXML();
|
||||
SQLXML sqlxml = this.connection.createSQLXML();
|
||||
SAXResult saxResult = sqlxml.setResult(SAXResult.class);
|
||||
ContentHandler contentHandler = saxResult.getHandler();
|
||||
contentHandler.startDocument();
|
||||
|
@ -95,7 +93,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
|
|||
String sql = "insert into " + getTableName()
|
||||
+ " (id, version, created_by, created_at, deleted, latest, name, type, state, asxml) values (?, ?, ?, ?, ?, ?, ?, ?, ?::order_state, ?)";
|
||||
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// id
|
||||
preparedStatement.setString(1, activity.getId());
|
||||
|
@ -140,7 +138,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
|
|||
|
||||
// and set the previous version to not be latest anymore
|
||||
sql = "update " + getTableName() + " SET latest = false WHERE id = ? AND version = ?";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// primary key
|
||||
preparedStatement.setString(1, activity.getId());
|
||||
|
@ -164,7 +162,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
|
|||
protected void internalUpdate(final Activity activity) {
|
||||
|
||||
// with versioning we save a new object
|
||||
if (tx().getRealm().isVersioningEnabled()) {
|
||||
if (this.versioningEnabled) {
|
||||
internalSave(activity);
|
||||
return;
|
||||
}
|
||||
|
@ -186,7 +184,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
|
|||
String sql = "update " + getTableName()
|
||||
+ " set created_by = ?, created_at = ?, deleted = ?, latest = ?, name = ?, type = ?, state = ?::order_state, asxml = ? where id = ? and version = ?";
|
||||
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// version
|
||||
preparedStatement.setString(1, activity.getVersion().getCreatedBy());
|
||||
|
|
|
@ -21,10 +21,7 @@ import javax.xml.parsers.SAXParserFactory;
|
|||
import javax.xml.transform.sax.SAXResult;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLXML;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.*;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Calendar;
|
||||
|
||||
|
@ -35,6 +32,7 @@ import li.strolch.model.xml.StrolchElementToSaxVisitor;
|
|||
import li.strolch.model.xml.XmlModelSaxReader;
|
||||
import li.strolch.persistence.api.OrderDao;
|
||||
import li.strolch.persistence.api.StrolchPersistenceException;
|
||||
import li.strolch.persistence.api.TransactionResult;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
|
@ -43,8 +41,8 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
|
|||
|
||||
public static final String ORDERS = "orders";
|
||||
|
||||
public PostgreSqlOrderDao(PostgreSqlStrolchTransaction tx) {
|
||||
super(tx);
|
||||
public PostgreSqlOrderDao(Connection connection, TransactionResult txResult, boolean versioningEnabled) {
|
||||
super(connection, txResult, versioningEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -79,7 +77,7 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
|
|||
}
|
||||
|
||||
protected SQLXML createSqlXml(Order order, PreparedStatement preparedStatement) throws SQLException, SAXException {
|
||||
SQLXML sqlxml = tx().getConnection().createSQLXML();
|
||||
SQLXML sqlxml = this.connection.createSQLXML();
|
||||
SAXResult saxResult = sqlxml.setResult(SAXResult.class);
|
||||
ContentHandler contentHandler = saxResult.getHandler();
|
||||
contentHandler.startDocument();
|
||||
|
@ -94,7 +92,7 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
|
|||
String sql = "insert into " + getTableName()
|
||||
+ " (id, version, created_by, created_at, deleted, latest, name, type, state, date, asxml) values (?, ?, ?, ?, ?, ?, ?, ?, ?::order_state, ?, ?)";
|
||||
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// id
|
||||
preparedStatement.setString(1, order.getId());
|
||||
|
@ -139,7 +137,7 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
|
|||
|
||||
// and set the previous version to not be latest anymore
|
||||
sql = "update " + getTableName() + " SET latest = false WHERE id = ? AND version = ?";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// primary key
|
||||
preparedStatement.setString(1, order.getId());
|
||||
|
@ -163,7 +161,7 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
|
|||
protected void internalUpdate(final Order order) {
|
||||
|
||||
// with versioning we save a new object
|
||||
if (tx().getRealm().isVersioningEnabled()) {
|
||||
if (this.versioningEnabled) {
|
||||
internalSave(order);
|
||||
return;
|
||||
}
|
||||
|
@ -185,7 +183,7 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
|
|||
// now we update the existing object
|
||||
String sql = "update " + getTableName()
|
||||
+ " set created_by = ?, created_at = ?, deleted = ?, latest = ?, name = ?, type = ?, state = ?::order_state, date = ?, asxml = ? where id = ? and version = ?";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// version
|
||||
preparedStatement.setString(1, order.getVersion().getCreatedBy());
|
||||
|
|
|
@ -61,6 +61,11 @@ public class PostgreSqlPersistenceHandler extends StrolchComponent implements Pe
|
|||
super.initialize(componentConfiguration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComponentConfiguration getConfiguration() {
|
||||
return super.getConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the map of {@link DataSource} which can be used in maintenance mode
|
||||
*
|
||||
|
@ -121,7 +126,7 @@ public class PostgreSqlPersistenceHandler extends StrolchComponent implements Pe
|
|||
return new PostgreSqlStrolchTransaction(getContainer(), realm, certificate, action, this);
|
||||
}
|
||||
|
||||
Connection getConnection(String realm) {
|
||||
public Connection getConnection(String realm) {
|
||||
DataSource ds = this.dsMap.get(realm);
|
||||
if (ds == null) {
|
||||
String msg = MessageFormat
|
||||
|
|
|
@ -21,10 +21,7 @@ import javax.xml.parsers.SAXParserFactory;
|
|||
import javax.xml.transform.sax.SAXResult;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLXML;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.*;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Calendar;
|
||||
|
||||
|
@ -35,6 +32,7 @@ import li.strolch.model.xml.StrolchElementToSaxVisitor;
|
|||
import li.strolch.model.xml.XmlModelSaxReader;
|
||||
import li.strolch.persistence.api.ResourceDao;
|
||||
import li.strolch.persistence.api.StrolchPersistenceException;
|
||||
import li.strolch.persistence.api.TransactionResult;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
|
@ -43,8 +41,8 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
|
|||
|
||||
public static final String RESOURCES = "resources";
|
||||
|
||||
protected PostgreSqlResourceDao(PostgreSqlStrolchTransaction tx) {
|
||||
super(tx);
|
||||
protected PostgreSqlResourceDao(Connection connection, TransactionResult txResult, boolean versioningEnabled) {
|
||||
super(connection, txResult, versioningEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -79,7 +77,7 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
|
|||
}
|
||||
|
||||
protected SQLXML createSqlXml(Resource res, PreparedStatement preparedStatement) throws SQLException, SAXException {
|
||||
SQLXML sqlxml = tx().getConnection().createSQLXML();
|
||||
SQLXML sqlxml = this.connection.createSQLXML();
|
||||
SAXResult saxResult = sqlxml.setResult(SAXResult.class);
|
||||
ContentHandler contentHandler = saxResult.getHandler();
|
||||
contentHandler.startDocument();
|
||||
|
@ -92,7 +90,7 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
|
|||
protected void internalSave(final Resource res) {
|
||||
String sql = "insert into " + getTableName()
|
||||
+ " (id, version, created_by, created_at, deleted, latest, name, type, asxml) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// id
|
||||
preparedStatement.setString(1, res.getId());
|
||||
|
@ -134,7 +132,7 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
|
|||
|
||||
// and set the previous version to not be latest anymore
|
||||
sql = "update " + getTableName() + " SET latest = false WHERE id = ? AND version = ?";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// primary key
|
||||
preparedStatement.setString(1, res.getId());
|
||||
|
@ -157,7 +155,7 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
|
|||
protected void internalUpdate(final Resource resource) {
|
||||
|
||||
// with versioning we save a new object
|
||||
if (tx().getRealm().isVersioningEnabled()) {
|
||||
if (this.versioningEnabled) {
|
||||
internalSave(resource);
|
||||
return;
|
||||
}
|
||||
|
@ -179,7 +177,7 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
|
|||
// now we update the existing object
|
||||
String sql = "update " + getTableName()
|
||||
+ " set created_by = ?, created_at = ?, deleted = ?, latest = ?, name = ?, type = ?, asxml = ? where id = ? and version = ?";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
// version
|
||||
preparedStatement.setString(1, resource.getVersion().getCreatedBy());
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
@ -17,18 +17,12 @@ package li.strolch.persistence.postgresql;
|
|||
|
||||
import java.sql.Connection;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import li.strolch.agent.api.ComponentContainer;
|
||||
import li.strolch.agent.api.StrolchRealm;
|
||||
import li.strolch.persistence.api.AbstractTransaction;
|
||||
import li.strolch.persistence.api.ActivityDao;
|
||||
import li.strolch.persistence.api.AuditDao;
|
||||
import li.strolch.persistence.api.OrderDao;
|
||||
import li.strolch.persistence.api.PersistenceHandler;
|
||||
import li.strolch.persistence.api.ResourceDao;
|
||||
import li.strolch.persistence.api.*;
|
||||
import li.strolch.privilege.model.Certificate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
||||
|
||||
|
@ -41,9 +35,8 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
|||
private AuditDao auditDao;
|
||||
private Connection connection;
|
||||
|
||||
public PostgreSqlStrolchTransaction(ComponentContainer container,
|
||||
StrolchRealm realm, Certificate certificate, String action,
|
||||
PostgreSqlPersistenceHandler persistenceHandler) {
|
||||
public PostgreSqlStrolchTransaction(ComponentContainer container, StrolchRealm realm, Certificate certificate,
|
||||
String action, PostgreSqlPersistenceHandler persistenceHandler) {
|
||||
super(container, realm, certificate, action);
|
||||
this.persistenceHandler = persistenceHandler;
|
||||
}
|
||||
|
@ -87,19 +80,19 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
|||
|
||||
OrderDao getOrderDao() {
|
||||
if (this.orderDao == null)
|
||||
this.orderDao = new PostgreSqlOrderDao(this);
|
||||
this.orderDao = new PostgreSqlOrderDao(getConnection(), getTxResult(), isVersioningEnabled());
|
||||
return this.orderDao;
|
||||
}
|
||||
|
||||
ResourceDao getResourceDao() {
|
||||
if (this.resourceDao == null)
|
||||
this.resourceDao = new PostgreSqlResourceDao(this);
|
||||
this.resourceDao = new PostgreSqlResourceDao(getConnection(), getTxResult(), isVersioningEnabled());
|
||||
return this.resourceDao;
|
||||
}
|
||||
|
||||
ActivityDao getActivityDao() {
|
||||
if (this.activityDao == null)
|
||||
this.activityDao = new PostgreSqlActivityDao(this);
|
||||
this.activityDao = new PostgreSqlActivityDao(getConnection(), getTxResult(), isVersioningEnabled());
|
||||
return this.activityDao;
|
||||
}
|
||||
|
||||
|
@ -110,9 +103,8 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
|||
}
|
||||
|
||||
Connection getConnection() {
|
||||
if (this.connection == null) {
|
||||
if (this.connection == null)
|
||||
this.connection = this.persistenceHandler.getConnection(getRealm().getRealm());
|
||||
}
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,11 +15,7 @@
|
|||
*/
|
||||
package li.strolch.persistence.postgresql;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLXML;
|
||||
import java.sql.*;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
|
@ -30,22 +26,23 @@ import li.strolch.model.StrolchRootElement;
|
|||
import li.strolch.model.Version;
|
||||
import li.strolch.persistence.api.StrolchDao;
|
||||
import li.strolch.persistence.api.StrolchPersistenceException;
|
||||
import li.strolch.persistence.api.TransactionResult;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public abstract class PostgresqlDao<T extends StrolchRootElement> implements StrolchDao<T> {
|
||||
|
||||
private PostgreSqlStrolchTransaction tx;
|
||||
protected Connection connection;
|
||||
protected final TransactionResult txResult;
|
||||
protected final boolean versioningEnabled;
|
||||
protected List<DaoCommand> commands;
|
||||
|
||||
public PostgresqlDao(PostgreSqlStrolchTransaction tx) {
|
||||
this.tx = tx;
|
||||
public PostgresqlDao(Connection connection, TransactionResult txResult, boolean versioningEnabled) {
|
||||
this.connection = connection;
|
||||
this.txResult = txResult;
|
||||
this.versioningEnabled = versioningEnabled;
|
||||
this.commands = new ArrayList<>();
|
||||
}
|
||||
|
||||
protected PostgreSqlStrolchTransaction tx() {
|
||||
return this.tx;
|
||||
}
|
||||
|
||||
protected abstract String getClassName();
|
||||
|
||||
protected abstract String getTableName();
|
||||
|
@ -55,7 +52,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
@Override
|
||||
public long querySize() {
|
||||
String sql = "select count(*) from " + getTableName() + " where latest = true";
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
try (ResultSet result = statement.executeQuery()) {
|
||||
result.next();
|
||||
|
@ -70,7 +67,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
@Override
|
||||
public long querySize(String type) {
|
||||
String sql = "select count(*) from " + getTableName() + " where type = ? and latest = true";
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
statement.setString(1, type);
|
||||
|
||||
try (ResultSet result = statement.executeQuery()) {
|
||||
|
@ -88,7 +85,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
Set<String> keySet = new HashSet<>();
|
||||
|
||||
String sql = "select distinct type from " + getTableName() + " where latest = true";
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
try (ResultSet result = statement.executeQuery()) {
|
||||
while (result.next()) {
|
||||
|
@ -108,7 +105,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
String sql = "select id, name, type, version, created_by, created_at, deleted, asxml from " + getTableName()
|
||||
+ " where type = ? and id = ? and version = ?";
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
statement.setString(1, type);
|
||||
statement.setString(2, id);
|
||||
statement.setInt(3, versionNr);
|
||||
|
@ -149,7 +146,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
List<T> list = new ArrayList<>(1);
|
||||
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
statement.setString(1, type);
|
||||
statement.setString(2, id);
|
||||
|
||||
|
@ -182,7 +179,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
String sql = "select count(*), max(version) from " + getTableName() + " where type = ? and id = ?";
|
||||
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
statement.setString(1, type);
|
||||
statement.setString(2, id);
|
||||
|
||||
|
@ -206,7 +203,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
String sql = "select count(*) from " + getTableName() + " where type = ? and id = ?";
|
||||
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
statement.setString(1, type);
|
||||
statement.setString(2, id);
|
||||
|
||||
|
@ -227,7 +224,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
String sql = "select id, name, type, version, created_by, created_at, deleted, asxml from " + getTableName()
|
||||
+ " where latest = true";
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
try (ResultSet result = statement.executeQuery()) {
|
||||
while (result.next()) {
|
||||
|
@ -261,7 +258,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
String sql = "select id, name, type, version, created_by, created_at, deleted, asxml from " + getTableName()
|
||||
+ " where type = ? and latest = true";
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
statement.setString(1, type);
|
||||
|
||||
try (ResultSet result = statement.executeQuery()) {
|
||||
|
@ -385,7 +382,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
// first find out how many there are
|
||||
long count = 0;
|
||||
String sql = "select count(*) from " + getTableName() + " where type = ? and id = ?";
|
||||
try (PreparedStatement statement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement statement = this.connection.prepareStatement(sql)) {
|
||||
statement.setString(1, element.getType());
|
||||
statement.setString(2, element.getId());
|
||||
|
||||
|
@ -405,7 +402,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
}
|
||||
|
||||
sql = "delete from " + getTableName() + " where id = ?";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
preparedStatement.setString(1, element.getId());
|
||||
|
||||
int modCount = preparedStatement.executeUpdate();
|
||||
|
@ -423,7 +420,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
private void internalRemoveVersion(T element) {
|
||||
String sql = "delete from " + getTableName() + " where type = ? and id = ? and version = ? and latest = true";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
preparedStatement.setString(1, element.getType());
|
||||
preparedStatement.setString(2, element.getId());
|
||||
preparedStatement.setInt(3, element.getVersion().getVersion());
|
||||
|
@ -437,7 +434,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
if (!element.getVersion().isFirstVersion()) {
|
||||
sql = "update " + getTableName() + " set latest = true where type = ? and id = ? and version = ?";
|
||||
try (PreparedStatement updateStmt = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement updateStmt = this.connection.prepareStatement(sql)) {
|
||||
int previousVersion = element.getVersion().getPreviousVersion();
|
||||
updateStmt.setString(1, element.getType());
|
||||
updateStmt.setString(2, element.getId());
|
||||
|
@ -463,7 +460,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
protected void internalRemoveAll() {
|
||||
String sql = "delete from " + getTableName();
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
|
||||
|
@ -475,7 +472,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
|
||||
protected void internalRemoveAllBy(String type) {
|
||||
String sql = "delete from " + getTableName() + " where type = ?";
|
||||
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {
|
||||
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
|
||||
preparedStatement.setString(1, type);
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
|
@ -490,7 +487,7 @@ public abstract class PostgresqlDao<T extends StrolchRootElement> implements Str
|
|||
public void flush() {
|
||||
// even though we support rollback we can clear the commands here even if we performed them because the DB transaction will be rolled back
|
||||
for (DaoCommand command : this.commands) {
|
||||
command.doComand(tx().getTxResult());
|
||||
command.doComand(this.txResult);
|
||||
}
|
||||
this.commands.clear();
|
||||
}
|
||||
|
|
|
@ -112,6 +112,7 @@ public class DbSchemaVersionCheck {
|
|||
Version currentVersion;
|
||||
try (Connection con = ds.getConnection()) {
|
||||
currentVersion = getCurrentVersion(con, this.app);
|
||||
con.commit();
|
||||
} catch (SQLException e) {
|
||||
String msg = "Failed to open DB connection to {0} due to: {1}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, ds, e.getMessage());
|
||||
|
|
Loading…
Reference in New Issue