dont fire multiple sql statments in batch because selects throwed an

exception
This commit is contained in:
Peter Hormanns 2014-11-25 12:21:48 +01:00
parent 4829a67fba
commit 2582919493
5 changed files with 44 additions and 45 deletions

View File

@ -15,50 +15,48 @@ public class JDBCProcessor extends AbstractProcessor {
private static final long serialVersionUID = 1L;
private String driver;
private String url;
private String user;
private String password;
private List<String> sql;
final private String driver;
final private String url;
final private String user;
final private String password;
final private List<String> sql;
private String errorMsg;
public JDBCProcessor(String driver, String url, String user, String password) {
public JDBCProcessor(final String driver, final String url, final String user, final String password) {
this.driver = driver;
this.url = url;
this.user = user;
this.password = password;
this.errorMsg = "";
this.sql = new ArrayList<String>();
}
public JDBCProcessor(String driver, String url) {
public JDBCProcessor(final String driver, final String url) throws ProcessorException {
this.driver = driver;
this.url = url;
final Config config = Config.getInstance();
if ("com.mysql.jdbc.Driver".equals(driver)) {
user = config.getProperty("mysqladmin.user", "root");
password = config.getProperty("mysqladmin.password");
} else {
if ("org.postgresql.Driver".equals(driver)) {
user = config.getProperty("pgsqladmin.user", "postgres");
password = config.getProperty("pgsqladmin.password");
} else {
throw new ProcessorException("database admin-user configuration failed");
}
}
this.errorMsg = "";
this.sql = new ArrayList<String>();
}
public void addSQL(String sqlStatement) {
if (sql == null)
sql = new ArrayList<String>();
sql.add(sqlStatement);
}
public Object process() throws ProcessorException {
Connection c = null;
Config config = Config.getInstance();
if ("com.mysql.jdbc.Driver".equals(driver)) {
if (user == null)
user = config.getProperty("mysqladmin.user", "root");
if (password == null)
password = config.getProperty("mysqladmin.password");
}
if ("org.postgresql.Driver".equals(driver)) {
if (user == null)
user = config.getProperty("pgsqladmin.user", "postgres");
if (password == null)
password = config.getProperty("pgsqladmin.password");
}
if (user == null || password == null) {
throw new ProcessorException("database admin-user configuration failed");
}
try {
Class.forName(driver);
c = DriverManager.getConnection(url, user, password);
@ -66,10 +64,10 @@ public class JDBCProcessor extends AbstractProcessor {
throw new ProcessorException("cannot connect to '" + url + "'");
Statement s = c.createStatement();
for (String sqlStatement : sql) {
s.addBatch(sqlStatement);
System.out.println("SQL: " + sqlStatement);
s.execute(sqlStatement);
}
return s.executeBatch();
return s.getUpdateCount();
} catch (SQLException aSqlExc) {
Exception exc = aSqlExc.getNextException();
if (exc == null) {
@ -82,10 +80,7 @@ public class JDBCProcessor extends AbstractProcessor {
throw new ProcessorException(aExc.getMessage());
} finally {
if (c != null) {
try {
c.close();
} catch (Exception exc) {
}
try { c.close(); } catch (Exception exc) { }
}
}
}

View File

@ -6,6 +6,7 @@ import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.qserv.EntityProcessorFactory;
import de.hsadmin.core.qserv.JDBCProcessor;
import de.hsadmin.core.qserv.Processor;
import de.hsadmin.core.qserv.ProcessorException;
/**
* Factory class which creates Processor instances for dealing with UNIX user
@ -20,12 +21,12 @@ public class MySqlDatabaseProcessorFactory implements EntityProcessorFactory {
"jdbc:mysql://localhost/" + database, user, password);
}
public static JDBCProcessor createMySqlAdminProcessor() {
public static JDBCProcessor createMySqlAdminProcessor() throws ProcessorException {
return new JDBCProcessor("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/");
}
public <T extends AbstractEntity> Processor createCreateProcessor(
EntityManager em, T entity) {
EntityManager em, T entity) throws ProcessorException {
Database db = (Database) entity;
assert db.getInstance().equals("mysql");
JDBCProcessor aJDBCP = null;
@ -38,7 +39,7 @@ public class MySqlDatabaseProcessorFactory implements EntityProcessorFactory {
return aJDBCP;
}
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) throws ProcessorException {
Database db = (Database) entity;
assert db.getInstance().equals("mysql");
String aName = AbstractEntity.escapeString(db.getName());
@ -49,7 +50,7 @@ public class MySqlDatabaseProcessorFactory implements EntityProcessorFactory {
return aJDBCP;
}
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) throws ProcessorException {
Database db = (Database) entity;
assert db.getInstance().equals("mysql");
JDBCProcessor aJDBCP = createMySqlAdminProcessor();

View File

@ -6,6 +6,7 @@ import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.qserv.EntityProcessorFactory;
import de.hsadmin.core.qserv.JDBCProcessor;
import de.hsadmin.core.qserv.Processor;
import de.hsadmin.core.qserv.ProcessorException;
/**
* Factory class which creates Processor instances for dealing with UNIX user
@ -15,7 +16,7 @@ import de.hsadmin.core.qserv.Processor;
*/
public class MySqlUserProcessorFactory implements EntityProcessorFactory {
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) throws ProcessorException {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("mysql");
JDBCProcessor aJDBCP = MySqlDatabaseProcessorFactory.createMySqlAdminProcessor();
@ -25,7 +26,7 @@ public class MySqlUserProcessorFactory implements EntityProcessorFactory {
return aJDBCP;
}
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) throws ProcessorException {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("mysql");
JDBCProcessor aJDBCP = MySqlDatabaseProcessorFactory.createMySqlAdminProcessor();
@ -35,7 +36,7 @@ public class MySqlUserProcessorFactory implements EntityProcessorFactory {
return aJDBCP;
}
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) throws ProcessorException {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("mysql");
JDBCProcessor aJDBCP = MySqlDatabaseProcessorFactory.createMySqlAdminProcessor();

View File

@ -6,6 +6,7 @@ import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.qserv.EntityProcessorFactory;
import de.hsadmin.core.qserv.JDBCProcessor;
import de.hsadmin.core.qserv.Processor;
import de.hsadmin.core.qserv.ProcessorException;
public class PgSqlDatabaseProcessorFactory implements EntityProcessorFactory {
@ -13,11 +14,11 @@ public class PgSqlDatabaseProcessorFactory implements EntityProcessorFactory {
return new JDBCProcessor("org.postgresql.Driver", "jdbc:postgresql://localhost/template1", user, password);
}
public static JDBCProcessor createPostgreSqlAdminProcessor() {
public static JDBCProcessor createPostgreSqlAdminProcessor() throws ProcessorException {
return new JDBCProcessor("org.postgresql.Driver", "jdbc:postgresql://localhost/template1");
}
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) throws ProcessorException {
Database db = (Database) entity;
assert db.getInstance().equals("pgsql");
JDBCProcessor aJDBCP = null;
@ -29,7 +30,7 @@ public class PgSqlDatabaseProcessorFactory implements EntityProcessorFactory {
return aJDBCP;
}
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) throws ProcessorException {
Database db = (Database) entity;
assert db.getInstance().equals("pgsql");
String aName = AbstractEntity.escapeString(db.getName());
@ -40,7 +41,7 @@ public class PgSqlDatabaseProcessorFactory implements EntityProcessorFactory {
return aJDBCP;
}
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) throws ProcessorException {
Database db = (Database) entity;
assert db.getInstance().equals("pgsql");
JDBCProcessor aJDBCP = createPostgreSqlAdminProcessor();

View File

@ -6,6 +6,7 @@ import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.qserv.EntityProcessorFactory;
import de.hsadmin.core.qserv.JDBCProcessor;
import de.hsadmin.core.qserv.Processor;
import de.hsadmin.core.qserv.ProcessorException;
/**
* Factory class which creates Processor instances for dealing with UNIX user
@ -15,7 +16,7 @@ import de.hsadmin.core.qserv.Processor;
*/
public class PgSqlUserProcessorFactory implements EntityProcessorFactory {
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) throws ProcessorException {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("pgsql");
JDBCProcessor aJDBCP = PgSqlDatabaseProcessorFactory.createPostgreSqlAdminProcessor();
@ -25,7 +26,7 @@ public class PgSqlUserProcessorFactory implements EntityProcessorFactory {
return aJDBCP;
}
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) throws ProcessorException {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("pgsql");
JDBCProcessor aJDBCP = PgSqlDatabaseProcessorFactory.createPostgreSqlAdminProcessor();
@ -35,7 +36,7 @@ public class PgSqlUserProcessorFactory implements EntityProcessorFactory {
return aJDBCP;
}
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) throws ProcessorException {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("pgsql");
JDBCProcessor aJDBCP = PgSqlDatabaseProcessorFactory.createPostgreSqlAdminProcessor();