Skip to content

Commit

Permalink
Check news table and index exists for each db operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Javatar81 committed Mar 7, 2022
1 parent 7d37f0b commit 127854c
Showing 1 changed file with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
Expand Down Expand Up @@ -50,37 +50,45 @@ public class NewsRepositoryJdbc implements NewsRepository{
@Inject
GeometryService geometryService;

private AtomicBoolean schemaCheckSuccess = new AtomicBoolean(false);

@Transactional
void startup(@Observes StartupEvent event) {
try {
createSchemaIfNotExists();
LOGGER.info("Checking whether NEWS table exist on startup");
createTableAndIndexIfNotExists();
LOGGER.info("Check successful");
} catch (SQLException e) {
// We do not rethrow exception because startup will fail
LOGGER.error("Creating schema at startup failed. Please make sure that schema is up to date", e);
LOGGER.error("Creating NEWS table at startup failed. Deferring its creation to next SQL operation", e);
}
}

private void createSchemaIfNotExists() throws SQLException {
if (!schemaCheckSuccess.get()) {
LOGGER.info("Checking if schema exists");
private void createTableAndIndexIfNotExists() throws SQLException {
if (!newsTableExists()) {
LOGGER.info("NEWS table does not exist");
LOGGER.info("Assuring that NEWS table and index exists");
try (Connection con = defaultDataSource.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(SQL_SCHEMA + SQL_GEO_INDEX)) {
preparedStatement.execute();
LOGGER.info("Created schema");
schemaCheckSuccess = new AtomicBoolean(true);
LOGGER.info("Successfully created NEWS table and index");
}
}
}

private boolean newsTableExists() throws SQLException {
try (Connection con = defaultDataSource.getConnection()) {
DatabaseMetaData meta = con.getMetaData();
ResultSet resultSet = meta.getTables(null, null, "news", new String[] {"TABLE"});
return resultSet.next();
}
}

@Override
@Transactional
public News create(News changed) {
LOGGER.info("Inserting into database");
try (Connection con = defaultDataSource.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(SQL_INSERT)) {
createSchemaIfNotExists();
createTableAndIndexIfNotExists();
preparedStatement.setString(1, changed.getTitle());
preparedStatement.setString(2, changed.getLink().toString());
preparedStatement.setString(3, changed.getDescription());
Expand Down Expand Up @@ -115,7 +123,7 @@ public News update(News changed) {
LOGGER.info("Updating news {} in database", changed.getId());
try (Connection con = defaultDataSource.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(SQL_UPDATE)) {
createSchemaIfNotExists();
createTableAndIndexIfNotExists();
preparedStatement.setString(1, changed.getTitle());
preparedStatement.setString(2, changed.getLink().toString());
preparedStatement.setString(3, changed.getDescription());
Expand Down Expand Up @@ -147,7 +155,7 @@ public List<News> findByGeometry(Rectangle bounds) {
LOGGER.info("Find by geo {}", bounds);
try (Connection con = defaultDataSource.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(SQL_FIND_BY_GEO)) {
createSchemaIfNotExists();
createTableAndIndexIfNotExists();
addGeoDatatypes(con);
preparedStatement.setObject(1, geometry);
ResultSet resultSet = preparedStatement.executeQuery();
Expand All @@ -166,7 +174,7 @@ public List<News> findByGeometry(Rectangle bounds) {
public Optional<News> findById(Long id) {
try (Connection con = defaultDataSource.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(SQL_FIND_BY_ID)) {
createSchemaIfNotExists();
createTableAndIndexIfNotExists();
addGeoDatatypes(con);
preparedStatement.setObject(1, id);

Expand Down Expand Up @@ -221,7 +229,7 @@ public void deleteAll()
{
try (Connection con = defaultDataSource.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(SQL_DELETE_ALL_NEWS)) {
createSchemaIfNotExists();
createTableAndIndexIfNotExists();
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RepositoryException(e);
Expand All @@ -232,7 +240,7 @@ public void deleteAll()
public Optional<News> findByLink(URL link) {
try (Connection con = defaultDataSource.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(SQL_FIND_BY_LINK)) {
createSchemaIfNotExists();
createTableAndIndexIfNotExists();
addGeoDatatypes(con);
preparedStatement.setObject(1, link.toString());

Expand Down

0 comments on commit 127854c

Please sign in to comment.