Skip to content

Commit

Permalink
Merge pull request #1 from phamvanlinh20111993/feature/improvement
Browse files Browse the repository at this point in the history
Feature/improvement
  • Loading branch information
phamvanlinh20111993 authored Sep 26, 2023
2 parents 5ec2a62 + 5b7ccaf commit 7405617
Show file tree
Hide file tree
Showing 38 changed files with 1,166 additions and 399 deletions.
Binary file not shown.
Binary file not shown.
37 changes: 31 additions & 6 deletions src/database/in/handle/AbstractSqlInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,42 @@

import database.in.utils.TransactionIsolationLevel;

/**
*
* @author PhamLinh
*
* @param <R>
*/
public abstract class AbstractSqlInsert<R> implements SqlInsert<R> {

private static final Logger logger = LoggerFactory.getLogger(AbstractSqlInsert.class);

protected DataSource dataSource;

protected TransactionIsolationLevel transactionIsolationLevel = TransactionIsolationLevel.TRANSACTION_READ_COMMITTED;


/**
*
* @param autoCommit
* @return
* @throws SQLException
*/
protected Connection getConnection(boolean autoCommit) throws SQLException {

if (dataSource == null)
if (dataSource == null) {
throw new NullPointerException("Datasource can not be null");
}

Connection conn = dataSource.getConnection();
conn.setAutoCommit(autoCommit);
return conn;
}


/**
*
* @param entity
* @param statement
* @throws SQLException
*/
protected void extractEntity(R entity, PreparedStatement statement) throws SQLException {
int index = 1;
for (Field field : entity.getClass().getDeclaredFields()) {
Expand All @@ -40,11 +58,18 @@ protected void extractEntity(R entity, PreparedStatement statement) throws SQLEx
Object valueObject = field.get(entity);
setPrepareStatement(statement, valueObject, index++);
} catch (IllegalArgumentException | IllegalAccessException e) {
logger.error("extractEntity(): {}", e.getMessage());
logger.error("AbstractSqlInsert.extractEntity(): {}", e.getMessage());
}
}
}


/**
*
* @param statement
* @param value
* @param index
* @throws SQLException
*/
protected void setPrepareStatement(PreparedStatement statement, Object value, int index) throws SQLException {

if(value == null) {
Expand Down
44 changes: 30 additions & 14 deletions src/database/in/handle/SimplePrepareStatementSqlInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import database.in.utils.InsertCodeStatus;
import database.in.utils.TransactionIsolationLevel;
import database.in.utils.Utils;

/**
*
* @author PhamLinh
*
* @param <T>
*/
public class SimplePrepareStatementSqlInsert<T> extends AbstractSqlInsert<T> {

private static final Logger logger = LoggerFactory.getLogger(SimplePrepareStatementSqlInsert.class);
Expand All @@ -28,7 +35,11 @@ public SimplePrepareStatementSqlInsert(DataSource dataSource, TransactionIsolati
this.dataSource = dataSource;
this.transactionIsolationLevel = transactionIsolationLevel;
}


/**
* {@inheritDoc} refer: https://www.baeldung.com/java-jdbc-auto-commit
* https://stackoverflow.com/questions/14625371/rollback-batch-execution-when-using-jdbc-with-autocommit-true
*/
@Override
public String singleInsertValue(T entity) {
logger.info("SimplePrepareStatementSqlInsert.singleInsertValue() start");
Expand All @@ -51,7 +62,11 @@ public String singleInsertValue(T entity) {
rs = statement.executeUpdate();
conn.commit();
} catch (SQLException e) {
System.err.println("SimplePrepareStatementSqlInsert.class singleInsertValue(): " + e.getMessage());
try {
conn.rollback();
} catch (SQLException e1) {
logger.error("SimplePrepareStatementSqlInsert.class batchInsertValues(): {}", e1.getMessage());
}
logger.error("SimplePrepareStatementSqlInsert.class singleInsertValue(): {}", e.getMessage());
} finally {
try {
Expand All @@ -62,20 +77,23 @@ public String singleInsertValue(T entity) {
conn.close();
}
} catch (SQLException e1) {
System.err.println("SimplePrepareStatementSqlInsert.class singleInsertValue(): " + e1.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class singleInsertValue(): {}", e1.getMessage());
}
}

if (rs > -1) {
return "Inserted " + rs;
return InsertCodeStatus.SUCCESS.getTypeValue() + " " + rs;
}

logger.info("SimplePrepareStatementSqlInsert.singleInsertValue() end");

return "fail single insert";
return InsertCodeStatus.FAIL.getTypeValue();
}

/**
* {@inheritDoc} refer: https://www.baeldung.com/java-jdbc-auto-commit
* https://stackoverflow.com/questions/14625371/rollback-batch-execution-when-using-jdbc-with-autocommit-true
*/
@Override
public String batchInsertValues(List<T> entities, boolean isForceInsert) {

Expand All @@ -93,8 +111,9 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
Connection conn = null;
PreparedStatement statement = null;
int[] res = null;
boolean isAutoCommit = !isForceInsert;
try {
conn = getConnection(isForceInsert);
conn = getConnection(isAutoCommit);
statement = conn.prepareStatement(sqlInsertStatement);
DatabaseMetaData dbmd = conn.getMetaData();
if (dbmd.supportsTransactionIsolationLevel(transactionIsolationLevel.getTypeValue())) {
Expand All @@ -106,17 +125,15 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
}
res = statement.executeBatch();
conn.commit();
return "Inserted " + Utils.sum(res);
return InsertCodeStatus.SUCCESS_FORCE_INSERT + " " + Utils.sum(res);
} catch (SQLException e) {
if (!isForceInsert) {
if (!isAutoCommit) {
try {
conn.rollback();
} catch (SQLException e1) {
System.err.println("SimplePrepareStatementSqlInsert.class batchInsertValues(): " + e1.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class batchInsertValues(): {}", e1.getMessage());
}
}
System.err.println("SimplePrepareStatementSqlInsert.class batchInsertValues(): " + e.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class batchInsertValues(): {}", e.getMessage());
} finally {
try {
Expand All @@ -127,21 +144,20 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
conn.close();
}
} catch (SQLException e1) {
System.err.println("SimplePrepareStatementSqlInsert.class batchInsertValues(): " + e1.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class batchInsertValues(): {}", e1.getMessage());
}
}

logger.info("SimplePrepareStatementSqlInsert.batchInsertValues() ends");

if (res != null) {
return "Inserted " + Utils.sum(res);
return InsertCodeStatus.SUCCESS_FORCE_INSERT + " " + Utils.sum(res);
}

if (isForceInsert) {
return "still insert despite some error";
return "Still insert despite some error";
}

return "Fail batch insert";
return InsertCodeStatus.FAIL_FORCE_INSERT.getTypeValue();
}
}
68 changes: 45 additions & 23 deletions src/database/in/handle/SimpleSqlInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import database.in.utils.InsertCodeStatus;
import database.in.utils.TransactionIsolationLevel;
import database.in.utils.Utils;

/**
*
* @author PhamLinh
*
* @param <T>
*/
public class SimpleSqlInsert<T> extends AbstractSqlInsert<T> {

private static final Logger logger = LoggerFactory.getLogger(SimpleSqlInsert.class);

public SimpleSqlInsert(DataSource dataSource) {
super();
this.dataSource = dataSource;
Expand All @@ -31,9 +38,13 @@ public SimpleSqlInsert(DataSource dataSource, TransactionIsolationLevel transact
this.transactionIsolationLevel = transactionIsolationLevel;
}

/**
* {@inheritDoc} refer: https://www.baeldung.com/java-jdbc-auto-commit
* https://stackoverflow.com/questions/14625371/rollback-batch-execution-when-using-jdbc-with-autocommit-true
*/
@Override
public String singleInsertValue(T entity) {
logger.info("SimpleSqlInsert.singleInsertValue() start");
logger.info("SimpleSqlInsert.singleInsertValue() start");
String insertQuery = this.createInsertPrefixCommand(entity) + SPACE + INSERT_VALUE_KEY + SPACE
+ this.createInsertSuffixCommand(entity);

Expand All @@ -53,7 +64,12 @@ public String singleInsertValue(T entity) {
result = statement.executeUpdate(insertQuery);

} catch (SQLException e) {
System.err.println(e.getMessage());
try {
conn.rollback();
} catch (SQLException e1) {
logger.error("SimplePrepareStatementSqlInsert.class batchInsertValues(): {}", e1.getMessage());
}
logger.error("SimpleSqlInsert.class singleInsertValue(): {}", e.getMessage());
} finally {
try {
if (statement != null) {
Expand All @@ -63,24 +79,28 @@ public String singleInsertValue(T entity) {
conn.close();
}
} catch (SQLException e1) {
System.err.println(e1.getMessage());
logger.error("SimpleSqlInsert.class singleInsertValue(): {}", e1.getMessage());
}
}

if (result > 0) {
return "inserted " + result;
return InsertCodeStatus.SUCCESS.getTypeValue() + " " + result;
}

logger.info("SimpleSqlInsert.singleInsertValue() end");

return "fail single inserted";
logger.info("SimpleSqlInsert.singleInsertValue() end");

return InsertCodeStatus.FAIL.getTypeValue();
}

/**
* {@inheritDoc} refer
* https://stackoverflow.com/questions/14625371/rollback-batch-execution-when-using-jdbc-with-autocommit-true
*/
@Override
public String batchInsertValues(List<T> entities, boolean isForceInsert) {
logger.info("SimpleSqlInsert.batchInsertValues() start");

logger.info("SimpleSqlInsert.batchInsertValues() start");

if (entities == null || entities.size() == 0) {
return EMPTY;
}
Expand All @@ -90,8 +110,10 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
Connection conn = null;
Statement statement = null;
int[] result = null;

boolean isAutoCommit = !isForceInsert;
try {
conn = getConnection(isForceInsert);
conn = getConnection(isAutoCommit);
statement = conn.createStatement();
DatabaseMetaData dbmd = conn.getMetaData();
if (dbmd.supportsTransactionIsolationLevel(transactionIsolationLevel.getTypeValue())) {
Expand All @@ -104,12 +126,12 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
}
result = statement.executeBatch();
} catch (SQLException e) {
System.err.println(e.getMessage());
if (!isForceInsert) {
logger.error("SimpleSqlInsert.class batchInsertValues(): {}", e.getMessage());
if (!isAutoCommit) {
try {
conn.rollback();
} catch (SQLException e1) {
System.err.println(e1.getMessage());
logger.error("SimpleSqlInsert.class batchInsertValues(): {}", e1.getMessage());
}
}
} finally {
Expand All @@ -121,21 +143,21 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
conn.close();
}
} catch (SQLException e1) {
System.err.println(e1.getMessage());
logger.error("SimpleSqlInsert.class batchInsertValues(): {}", e1.getMessage());
}
}

if (result != null) {
return "inserted " + Utils.sum(result);
return InsertCodeStatus.SUCCESS_FORCE_INSERT.getTypeValue() + " " + Utils.sum(result);
}

if (isForceInsert) {
return "still insert despite some error";
return InsertCodeStatus.FAIL_FORCE_INSERT.getTypeValue();
}

logger.info("SimpleSqlInsert.batchInsertValues() end");

return "fail batch inserted";
logger.info("SimpleSqlInsert.batchInsertValues() end");

return InsertCodeStatus.FAIL_FORCE_INSERT.getTypeValue();
}

/**
Expand Down Expand Up @@ -169,7 +191,7 @@ public String createInsertSuffixCommand(T entity) {
insertsuffixCommand.append(this.protectValue(value));
insertsuffixCommand.append(COMMA);
} catch (IllegalArgumentException | IllegalAccessException e) {
System.err.println(e.getMessage());
logger.error("SimpleSqlInsert.class createInsertSuffixCommand(): {}", e.getMessage());
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/database/in/utils/InsertCodeStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package database.in.utils;

public enum InsertCodeStatus {
SUCCESS("Success Inserted "),
FAIL("Fail Inserted"),
SUCCESS_FORCE_INSERT("Success with Batch Inserted"),
FAIL_FORCE_INSERT("Fail with Batch Inserted");

private String codeValue;

private InsertCodeStatus(String codeValue) {
this.codeValue = codeValue;
}

public String getTypeValue() {
return this.codeValue;
}
}
Loading

0 comments on commit 7405617

Please sign in to comment.