shuber 2005/04/29 17:08:02 CEST
Modified files:
. jahia_update.jpx
src/java/org/jahia/update/db DBManager.java
Log:
- Updated DBManager to make it more configurable, for example to be able to
take configuration paths so that we could execute it from another directory
than the Jahia directory (mostly useful to develop test cases)
- Updated alterTable method to use batch statements, but it is still very
slow.
Revision Changes Path
1.3 +21 -4 jahia_update/jahia_update.jpx
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia_update/jahia_update.jpx.diff?r1=1.2&r2=1.3&f=h
1.6 +73 -26 jahia_update/src/java/org/jahia/update/db/DBManager.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia_update/src/java/org/jahia/update/db/DBManager.java.diff?r1=1.5&r2=1.6&f=h
Index: jahia_update.jpx
===================================================================
RCS file: /home/cvs/repository/jahia_update/jahia_update.jpx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- jahia_update.jpx 22 Apr 2005 15:18:12 -0000 1.2
+++ jahia_update.jpx 29 Apr 2005 15:08:02 -0000 1.3
@@ -1,17 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--JBuilder XML Project-->
<project>
+ <property category="generalFormatting" name="baseCodeStyle"
value="java/Jahia.codestyle"/>
<property category="generalFormatting" name="blockIndent" value="4"/>
<property category="generalFormatting" name="tabSize" value="4"/>
+ <property category="generalFormatting2" name="blockIndent" value="4"/>
+ <property category="generalFormatting2" name="overrideBasicFormatting"
value="1"/>
+ <property category="generalFormatting2" name="tabSize" value="4"/>
+ <property category="javaFormatting" name="alignMultilineAssign" value="1"/>
+ <property category="javaFormatting" name="alwaysClasses" value="1"/>
+ <property category="javaFormatting" name="arrayInitDataOnNewLine"
value="0"/>
<property category="javaFormatting" name="catchOnNewLine" value="0"/>
<property category="javaFormatting" name="elseOnNewLine" value="0"/>
<property category="javaFormatting" name="extendsOnNewLine" value="0"/>
<property category="javaFormatting" name="finallyOnNewLine" value="0"/>
- <property category="javaFormatting" name="packageThreshold" value="0"/>
+ <property category="javaFormatting" name="implementsOnNewLine" value="0"/>
+ <property category="javaFormatting" name="packagePrefixGroups"
value="java;javax;BLANK_LINE;java.awt;javax.swing;BLANK_LINE;org;(*)"/>
+ <property category="javaFormatting" name="spaceBeforeMethodDefLparen"
value="1"/>
+ <property category="javaFormatting" name="throwsOnNewLine" value="1"/>
<property category="javaFormatting" name="whileOnNewLine" value="0"/>
<property category="javadoc" name="custom.tags.1" value="todo;a;To Do:"/>
+ <property category="runtime" name="ConfigurationCount" value="1"/>
<property category="runtime" name="DefaultConfiguration" value="-1"/>
- <property category="runtime" name="ExcludeDefaultForZero" value="1"/>
+ <property category="runtime.0" name="BuildTargetOnRun"
value="com.borland.jbuilder.build.ProjectBuilder$ProjectBuildAction;make"/>
+ <property category="runtime.0" name="ConfigurationName" value="Jahia
Update main runtime"/>
+ <property category="runtime.0" name="RunnableType"
value="com.borland.jbuilder.runtime.ApplicationRunner"/>
+ <property category="runtime.0" name="application.class"
value="org.jahia.update.JahiaUpdate"/>
+ <property category="runtime.1" name="ConfigurationName"
value="TestPatch4_0_6"/>
+ <property category="runtime.1" name="RunnableType"
value="com.borland.jbuilder.runtime.TestRunner"/>
+ <property category="runtime.1" name="test.class"
value="org.jahia.update.test.core.TestPatch4_0_6"/>
<property category="serverservices" name="disabled.services"
value="jdatastore"/>
<property category="serverservices" name="single.server.name"
value="Tomcat 4.0"/>
<property category="sys" name="AuthorLabel" value="@author"/>
@@ -34,8 +51,8 @@
<property category="sys" name="Libraries" value="JUnit"/>
<property category="sys" name="MakeStable" value="0"/>
<property category="sys" name="OutPath" value="target/classes"/>
- <property category="sys" name="SourcePath" value="src/java"/>
- <property category="sys" name="TestPath" value="src/java"/>
+ <property category="sys" name="SourcePath"
value="src/java;src/test/java;src/test/resources"/>
+ <property category="sys" name="TestPath" value="src/test/java"/>
<property category="sys" name="Title" value=""/>
<property category="sys" name="TitleLabel" value="Title:"/>
<property category="sys" name="Version" value="1.0"/>
Index: DBManager.java
===================================================================
RCS file:
/home/cvs/repository/jahia_update/src/java/org/jahia/update/db/DBManager.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- DBManager.java 22 Apr 2005 15:18:13 -0000 1.5
+++ DBManager.java 29 Apr 2005 15:08:02 -0000 1.6
@@ -71,6 +71,10 @@
if (_dbManager == null) {
try {
// Load and init the DB driver.
+ _pathToJahiaDeployment =
jahiaProps.getProperty("pathToJahiaDeployment");
+ if (jahiaProps.getProperty("db_batchSize") != null) {
+ _batchLimit =
Integer.parseInt(jahiaProps.getProperty("db_batchSize"));
+ }
loadDBDriver(jahiaProps);
_dbScriptFileName = jahiaProps.getProperty("db_script");
} catch (Exception e) {
@@ -243,6 +247,9 @@
* This method is used as a replacement for the ALTER TABLE SQL command
* which is not an entirely SQL standard and varies from one database to
* another (and some even don't support it at all).
+ *
+ * Note : this method can be quite slow if there is a lot of data.
+ * @param tableName the name of the table to alter
*/
public void alterTable(String tableName) {
try {
@@ -252,42 +259,57 @@
String tempTableName = tableName + "_tmp";
- PreparedStatement testTempTableExistenceQuery = prepareStatement(
- "SELECT * FROM " + tempTableName);
- ResultSet columnsResultSet =
testTempTableExistenceQuery.executeQuery();
- if (columnsResultSet.next()) {
- // a table exists that has the same name as our temporary
table,
- // let's output a warning and abort immediately.
- System.out.println("Aborting alterTable operation as a table
called " + tempTableName + " already exists");
- return;
+ try {
+ PreparedStatement testTempTableExistenceQuery =
prepareStatement(
+ "SELECT * FROM " + tempTableName);
+ ResultSet columnsResultSet =
testTempTableExistenceQuery.executeQuery();
+ if (columnsResultSet.next()) {
+ // a table exists that has the same name as our
temporary table,
+ // let's output a warning and abort immediately.
+ System.out.println("Aborting alterTable operation as a
table called " +
+ tempTableName + " already exists");
+ return;
+ }
+ } catch (SQLException sqle) {
+ // an SQL exception will be thrown if the table doesn't
exist,
+ // which should be the default case, so we just ignore it.
}
-
// now that we've made sure that no table exists that would
conflict
// with the temporary table, we create the temporary table.
- PreparedStatement tmpTableCreate = prepareStatement(
- getTableCreation(tableName));
+ String tempTableCreationSQL = getTableCreation(tableName);
+ int tableNamePos = tempTableCreationSQL.indexOf(tableName);
+
+ tempTableCreationSQL = tempTableCreationSQL.substring(0,
tableNamePos) +
+ tempTableName +
tempTableCreationSQL.substring(tableNamePos + tableName.length());
+
+ System.out.println("Creating temporary table " + tempTableName +
"...");
+ PreparedStatement tmpTableCreate =
prepareStatement(tempTableCreationSQL);
tmpTableCreate.execute();
// now let's copy all the data into the temporary table
-
+ System.out.println("Copying table data from " + tableName + " to
" + tempTableName + "...");
copyTableData(tableName, tempTableName);
// now we will drop the source table, and re-create it with the
new
// schema definition
+ System.out.println("Droping table " + tableName);
PreparedStatement tableDrop = prepareStatement("DROP TABLE " +
tableName);
tableDrop.execute();
+ System.out.println("Creating table " + tableName + " with new
schema definition");
PreparedStatement tableCreate =
prepareStatement(getTableCreation(
tableName));
tableCreate.execute();
// now we copy from the temporary table to the new final table
+ System.out.println("Copying table data from " + tempTableName +
" to " + tableName + "...");
copyTableData(tempTableName, tableName);
// now we drop the temporary table
+ System.out.println("Droping table " + tempTableName);
PreparedStatement tmpTableDrop = prepareStatement("DROP TABLE " +
- tableName);
+ tempTableName);
tmpTableDrop.execute();
} catch (Exception e) {
@@ -333,10 +355,12 @@
valuesBuff.append(" )");
String sqlInsertStatement = buff.toString() +
valuesBuff.toString();
- PreparedStatement pstmt = null;
+ PreparedStatement pstmt = prepareStatement(sqlInsertStatement);
+ pstmt.clearBatch();
+ pstmt.clearParameters();
+ int batchCounter = 0;
while (columnsResultSet.next()) {
try {
- pstmt = prepareStatement(sqlInsertStatement);
for (int column = 1; column <= columnCount; column++) {
String column_name = columnsMetaData.
getColumnLabel(column).toLowerCase();
@@ -344,17 +368,23 @@
getObject(column);
pstmt.setObject(column, column_value);
}
- pstmt.executeUpdate();
+ pstmt.addBatch();
+ batchCounter++;
+ if (batchCounter == _batchLimit) {
+ pstmt.executeBatch();
+ pstmt.clearBatch();
+ batchCounter = 0;
+ }
} catch (Throwable t) {
t.printStackTrace();
} finally {
- if (pstmt != null) {
- try {
- pstmt.close();
- } catch (Throwable t) {
- t.printStackTrace();
- }
- }
+ }
+ }
+ if (pstmt != null) {
+ try {
+ pstmt.close();
+ } catch (Throwable t) {
+ t.printStackTrace();
}
}
} catch (IndexOutOfBoundsException ioobe) {
@@ -392,8 +422,16 @@
throws Exception {
// Get all the DB libraries candidates
- String jahiaLibsDirectory = "WEB-INF" + File.separator + "lib";
+ String jahiaLibsDirectory = null;
+ if (_pathToJahiaDeployment != null) {
+ jahiaLibsDirectory = _pathToJahiaDeployment + File.separator +
"WEB-INF" + File.separator + "lib";
+ } else {
+ jahiaLibsDirectory = "WEB-INF" + File.separator + "lib";
+ }
File[] jahiaLibs = (new File(jahiaLibsDirectory)).listFiles();
+ if (jahiaLibs == null) {
+ throw new Exception("Jahia deployment directory couldn't be
found, make sure you launch update from the Jahia deployment directory");
+ }
ArrayList jahiaLibsURL = new ArrayList();
for (int i = 0; i < jahiaLibs.length; i++) {
if (jahiaLibs[i].getPath().endsWith("jahia.jar")) continue;
@@ -421,8 +459,13 @@
throws Exception {
// Load the corresponding DB script file
- scriptFile = "WEB-INF" + File.separator + "var" + File.separator +
- "db" + File.separator + scriptFile;
+ if (_pathToJahiaDeployment != null) {
+ scriptFile = _pathToJahiaDeployment + File.separator + "WEB-INF"
+ File.separator + "var" + File.separator +
+ "db" + File.separator + scriptFile;
+ } else {
+ scriptFile = "WEB-INF" + File.separator + "var" + File.separator
+
+ "db" + File.separator + scriptFile;
+ }
System.out.println("Load script : " + scriptFile);
BufferedReader jahiaDBScriptBuffer = new BufferedReader(new
FileReader(scriptFile));
String tableCreation;
@@ -464,4 +507,8 @@
private static String _dbScriptFileName;
private static Connection _dbConnection;
+
+ private static String _pathToJahiaDeployment = null;
+
+ private static int _batchLimit = 100;
}