diff -rc orig/jboss-3.0.2-src/server/src/etc/conf/default/standardjbosscmp-jdbc.xml new/jboss-3.0.2-src/server/src/etc/conf/default/standardjbosscmp-jdbc.xml *** orig/jboss-3.0.2-src/server/src/etc/conf/default/standardjbosscmp-jdbc.xml Sun Jul 14 21:01:36 2002 --- new/jboss-3.0.2-src/server/src/etc/conf/default/standardjbosscmp-jdbc.xml Mon Sep 16 11:45:29 2002 *************** *** 29,34 **** --- 29,35 ---- * 1000 + false *************** *** 37,42 **** --- 38,44 ---- SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 116,121 **** --- 118,124 ---- CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 202,207 **** --- 205,211 ---- SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 30 *************** *** 328,333 **** --- 332,338 ---- SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 30 *************** *** 462,467 **** --- 467,473 ---- SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 30 *************** *** 591,596 **** --- 597,603 ---- CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 673,678 **** --- 680,686 ---- SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 795,800 **** --- 803,809 ---- SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 917,922 **** --- 926,932 ---- CONSTRAINT ?1 PRIMARY KEY (?2) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1024,1029 **** --- 1034,1040 ---- CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1088,1093 **** --- 1099,1105 ---- PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1162,1167 **** --- 1174,1180 ---- SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1283,1288 **** --- 1296,1302 ---- SELECT ?1 FROM ?2 with (rowlock) WHERE ?3 CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1405,1410 **** --- 1419,1425 ---- SELECT ?1 FROM ?2 with (rowlock) WHERE ?3 CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1541,1546 **** --- 1556,1562 ---- CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1622,1627 **** --- 1638,1644 ---- SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1701,1706 **** --- 1718,1724 ---- CONSTRAINT ?1 PRIMARY KEY (?2) ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5) + CREATE INDEX ?1 ON ?2 (?3) t _ 32 *************** *** 1786,1791 **** --- 1804,1810 ---- PRIMARY KEY (?2) CONSTRAINT ?1 ALTER TABLE ?1 ADD CONSTRAINT FOREIGN KEY (?3) REFERENCES ?4 (?5) CONSTRAINT ?2 + CREATE INDEX ?1 ON ?2 (?3) t _ 32 diff -rc orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCStartCommand.java new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCStartCommand.java *** orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCStartCommand.java Tue Jun 11 20:07:36 2002 --- new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCStartCommand.java Thu Sep 19 10:42:15 2002 *************** *** 85,90 **** --- 85,92 ---- dataSource, entity.getTableName(), getEntityCreateTableSQL(dataSource)); + createIndexes(dataSource, entity.getTableName(), + entity.getIndexes()) ; } else { log.debug("Table not create as requested: " + entity.getTableName()); *************** *** 399,402 **** --- 401,572 ---- // success log.info("Added foreign key constraint to table '" + tableName + "'" ); } + + /** + * This method creates any indexes specified in the current meta data. + * @param dataSource the data source to use. + * @param tableName the current table name. + * @param indexes the list of specified indexes. + */ + private void createIndexes(final DataSource dataSource, + final String tableName, final Map indexes) + throws DeploymentException + { + final int numIndexes = indexes.size() ; + if (numIndexes > 0) + { + final ArrayList indexSql = new ArrayList(numIndexes) ; + final Iterator indexIter = indexes.keySet().iterator() ; + do + { + final String indexName = (String)indexIter.next() ; + final ArrayList fields = (ArrayList)indexes.get(indexName) ; + final String sql = generateIndexSql(dataSource, indexName, fields) ; + indexSql.add(sql) ; + } + while(indexIter.hasNext()) ; + + executeSql(dataSource, indexSql) ; + } + } + + /** + * Generate the SQL for creating an index. + * @param dataSource the current data source. + * @param indexName the name of the index. + * @param fields the fields in the index. + * @return the sql for creating the index. + */ + private String generateIndexSql(final DataSource dataSource, + final String indexName, final ArrayList fields) + throws DeploymentException + { + final JDBCFunctionMappingMetaData indexTemplate = + manager.getMetaData().getTypeMapping().getIndexTemplate() ; + if(indexTemplate == null) + { + throw new IllegalStateException("Indexing is not " + + "allowed for this type of datastore"); + } + final String fieldInfo = SQLUtil.getColumnNamesClause(fields); + final String tableName = entity.getTableName() ; + + final String[] args = { + SQLUtil.fixIndexName("ix_"+tableName+"_"+indexName, dataSource), + tableName, + fieldInfo + } ; + return indexTemplate.getFunctionSql(args) ; + } + + /** + * Execute a list of SQL commands. + * This method executes a list of SQL commands in a single transaction. + * + * @param dataSource the data source to use. + * @param sqlList the list of sql statements to be executed. + * + * @throws DeploymentException on transaction or database errors. + */ + private void executeSql(final DataSource dataSource, + final List sqlList) + throws DeploymentException + { + final int numSqlStatements = sqlList.size() ; + if (numSqlStatements > 0) + { + // since we use the pools, we have to do this within a transaction + // suspend the current transaction + final TransactionManager tm = manager.getContainer().getTransactionManager(); + + final Transaction oldTransaction ; + try + { + oldTransaction = tm.suspend() ; + } + catch(final Exception e) + { + throw new DeploymentException("Could not suspend current " + + "transaction before executing sql.", e); + } + + try + { + final Connection con = dataSource.getConnection(); + try + { + con.setAutoCommit(false) ; + final boolean batchUpdate = con.getMetaData().supportsBatchUpdates() ; + + final Statement statement = con.createStatement(); + try + { + final Iterator sqlIter = sqlList.iterator() ; + do + { + final String sql = (String)sqlIter.next() ; + log.debug("Executing SQL: " + sql); + + if (batchUpdate) + { + statement.addBatch(sql) ; + } + else + { + statement.executeUpdate(sql); + } + } + while (sqlIter.hasNext()) ; + + if (batchUpdate) + { + statement.executeBatch() ; + } + } + finally + { + JDBCUtil.safeClose(statement) ; + } + con.commit() ; + } + catch (final Exception e) + { + try + { + con.rollback() ; + } + catch (final Exception ex) + { + log.error("Error during rollback", ex) ; + } + throw e ; + } + finally + { + JDBCUtil.safeClose(con) ; + } + } + catch(final Exception e) + { + log.warn("Could not execute sql: table=" + entity.getTableName()) ; + throw new DeploymentException("Error while executing sql", e) ; + } + finally + { + try + { + // resume the old transaction + if (oldTransaction != null) + { + tm.resume(oldTransaction) ; + } + } + catch (final Exception e) + { + throw new DeploymentException("Could not reattach original " + + "transaction after executing sql") ; + } + } + } + } } diff -rc orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/SQLUtil.java new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/SQLUtil.java *** orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/SQLUtil.java Sun Apr 14 19:59:30 2002 --- new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/SQLUtil.java Wed Sep 18 16:53:05 2002 *************** *** 76,81 **** --- 76,86 ---- return fixTableName(name, dataSource); } + public static String fixIndexName(final String name, final DataSource dataSource) + throws DeploymentException { + return fixTableName(name, dataSource) ; + } + // ======================================================================= // Create Table Columns Clause // columnName0 sqlType0 diff -rc orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCEntityBridge.java new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCEntityBridge.java *** orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCEntityBridge.java Sun Aug 25 21:44:00 2002 --- new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCEntityBridge.java Wed Sep 18 22:09:49 2002 *************** *** 84,89 **** --- 84,91 ---- private List eagerLoadFields; private List lazyLoadGroups; + private Map indexes; + public JDBCEntityBridge( JDBCEntityMetaData metadata, JDBCStoreManager manager) throws DeploymentException { *************** *** 122,127 **** --- 124,132 ---- // ejbSelect methods loadSelectors(metadata); + + // load the index information + loadIndexes(metadata) ; } public void resolveRelationships() throws DeploymentException { *************** *** 304,309 **** --- 309,348 ---- selectorsByMethod = Collections.unmodifiableMap(selectorsByMethod); } + private void loadIndexes(final JDBCEntityMetaData metadata) + throws DeploymentException { + final Map metaIndexes = metadata.getIndexes() ; + final int numIndexes = metaIndexes.size() ; + + if (numIndexes == 0) + { + indexes = metaIndexes ; + } + else + { + final HashMap newIndexes = new HashMap() ; + final Iterator metaIndexesIter = metaIndexes.keySet().iterator() ; + do + { + final String indexName = (String)metaIndexesIter.next() ; + final List fields = (List)metaIndexes.get(indexName) ; + final ArrayList newFields = new ArrayList(fields.size()) ; + final Iterator fieldIter = fields.iterator() ; + do + { + final String fieldName = (String)fieldIter.next() ; + final JDBCCMPFieldBridge fieldBridge = getCMPFieldByName(fieldName) ; + newFields.add(fieldBridge) ; + } + while(fieldIter.hasNext()) ; + newIndexes.put(indexName, newFields) ; + } + while(metaIndexesIter.hasNext()) ; + + indexes = Collections.unmodifiableMap(newIndexes) ; + } + } + public String getEntityName() { return metadata.getName(); } *************** *** 448,453 **** --- 487,500 ---- public Collection getSelectors() { return selectorsByMethod.values(); } + + public boolean getGenerateIndexes() { + return metadata.getGenerateIndexes() ; + } + + public Map getIndexes() { + return indexes ; + } public void initInstance(EntityEnterpriseContext ctx) { for(Iterator iter = fields.iterator(); iter.hasNext();) { diff -rc orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCEntityMetaData.java new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCEntityMetaData.java *** orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCEntityMetaData.java Sun Aug 25 21:43:06 2002 --- new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCEntityMetaData.java Wed Sep 18 21:02:03 2002 *************** *** 18,23 **** --- 18,24 ---- import java.util.Set; import org.jboss.deployment.DeploymentException; + import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge; import org.jboss.metadata.EntityMetaData; import org.jboss.metadata.MetaData; import org.jboss.metadata.QueryMetaData; *************** *** 98,103 **** --- 99,109 ---- private final boolean createTable; /** + * Should we try and generate the indexes when deployed? + */ + private final boolean generateIndexes; + + /** * Should we drop the table when undeployed? */ private final boolean removeTable; *************** *** 162,167 **** --- 168,178 ---- private final Map queries = new HashMap(); /** + * A map of the indexes on this entity by the index name. + */ + private final Map indexes ; + + /** * The factory used to used to create query meta data */ private final JDBCQueryMetaDataFactory queryFactory; *************** *** 309,314 **** --- 320,326 ---- dataSourceName = null; datasourceMapping = null; createTable = false; + generateIndexes = false; removeTable = false; rowLocking = false; primaryKeyConstraint = false; *************** *** 315,320 **** --- 327,333 ---- readOnly = false; readTimeOut = -1; readAhead = JDBCReadAheadMetaData.DEFAULT; + indexes = Collections.EMPTY_MAP ; } /** *************** *** 560,565 **** --- 573,606 ---- // overrides defaults added above queries.putAll(newQueries); } + + // indexes + final Map indexesMap = loadIndexesXml(element); + + if (indexesMap == null) { + indexes = defaultValues.indexes ; + } else { + final HashMap newIndexes = new HashMap(defaultValues.indexes) ; + newIndexes.putAll(indexesMap) ; + indexes = Collections.unmodifiableMap(newIndexes) ; + } + + /* + * We only generate indexes if we are creating the entity table + * for EJB2 CMP and explicit indexes have not been included. + * Use the specified value or default one if not provided. + */ + if (createTable && isCMP2x() && (indexes.size() == 0)) { + final String generateIndexesStr = + MetaData.getOptionalChildContent(element, "generate-indexes"); + if(generateIndexesStr != null) { + generateIndexes = Boolean.valueOf(generateIndexesStr).booleanValue(); + } else { + generateIndexes = defaultValues.getGenerateIndexes(); + } + } else { + generateIndexes = false ; + } } /** *************** *** 661,666 **** --- 702,758 ---- } } + + /** + * Load the list of indexes from the xml element. + * @param element the containing element + * @return true if indexes have been specified, false otherwise. + */ + private Map loadIndexesXml(final Element element) + throws DeploymentException + { + final Element indexesElement = MetaData.getOptionalChild(element, "indexes") ; + + if (indexesElement == null) + { + return null ; + } + + final HashMap indexesMap = new HashMap() ; + + // Get all indexes + final Iterator indexIter = + MetaData.getChildrenByTagName(indexesElement, "index") ; + + while(indexIter.hasNext()) + { + final Element indexElement = (Element)indexIter.next() ; + final String indexName = MetaData.getUniqueChildContent(indexElement, "index-name") ; + + final Element fieldsElement = MetaData.getUniqueChild(indexElement, "fields") ; + final Iterator fieldIter = MetaData.getChildrenByTagName(fieldsElement, "field-name") ; + final ArrayList fields = new ArrayList() ; + + while(fieldIter.hasNext()) + { + final Element fieldElement = (Element)fieldIter.next() ; + final String fieldName = MetaData.getElementContent(fieldElement) ; + + final JDBCCMPFieldMetaData fieldMetaData = getCMPFieldByName(fieldName) ; + + if (fieldMetaData == null) + { + throw new DeploymentException("Unknown field-name specified in index (" + + indexName + ", " + fieldName + ")") ; + } + fields.add(fieldName) ; + } + + indexesMap.put(indexName, fields) ; + } + + return indexesMap ; + } /** * Gets the meta data for the application of which this entity is a member. *************** *** 974,979 **** --- 1066,1091 ---- } /** + * Gets the flag used to determine if the store manager should attempt to + * generate indexes when creating the table during entity bean deployment. + * @return true if the store manager should attempt to generate indexes. + */ + public boolean getGenerateIndexes() { + return generateIndexes; + } + + /** + * Get the explicit indexes. + * This method returns a map which is keyed using the index name + * and has a value that is a list of CMP field names. + * + * @return The indexes. + */ + public Map getIndexes() { + return indexes; + } + + /** * Compares this JDBCEntityMetaData against the specified object. Returns * true if the objects are the same. Two JDBCEntityMetaData are the same * if they both have the same name and are defined in the same application. diff -rc orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCTypeMappingMetaData.java new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCTypeMappingMetaData.java *** orig/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCTypeMappingMetaData.java Sun Aug 25 21:43:26 2002 --- new/jboss-3.0.2-src/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCTypeMappingMetaData.java Wed Sep 18 17:17:42 2002 *************** *** 47,52 **** --- 47,53 ---- private JDBCFunctionMappingMetaData rowLocking = null; private JDBCFunctionMappingMetaData fkConstraint = null; private JDBCFunctionMappingMetaData pkConstraint = null; + private JDBCFunctionMappingMetaData index = null; /** * Constructs a mapping with the data contained in the type-mapping xml *************** *** 91,96 **** --- 92,105 ---- fkConstraintSQL); } + // fk constraint + final String indexSQL = + MetaData.getUniqueChildContent(element, "index-template"); + if(indexSQL != null && !indexSQL.trim().equals("")) + { + index = new JDBCFunctionMappingMetaData("index", indexSQL); + } + // get the mappings Iterator iterator = MetaData.getChildrenByTagName(element, "mapping"); *************** *** 292,297 **** --- 301,314 ---- return fkConstraint; } + /** + * Returns index SQL template. + */ + public JDBCFunctionMappingMetaData getIndexTemplate() + { + return index; + } + private void addDefaultFunctionMapping() { JDBCFunctionMappingMetaData function; diff -rc orig/jboss-3.0.2-src/server/src/resources/org/jboss/metadata/jbosscmp-jdbc_3_0.dtd new/jboss-3.0.2-src/server/src/resources/org/jboss/metadata/jbosscmp-jdbc_3_0.dtd *** orig/jboss-3.0.2-src/server/src/resources/org/jboss/metadata/jbosscmp-jdbc_3_0.dtd Sun Aug 25 21:45:52 2002 --- new/jboss-3.0.2-src/server/src/resources/org/jboss/metadata/jbosscmp-jdbc_3_0.dtd Thu Sep 19 09:30:01 2002 *************** *** 20,26 **** + + + + *************** *** 171,177 **** remove-table?, read-only?, read-time-out?, row-locking?, pk-constraint?, read-ahead?, list-cache-max?, fetch-size?, table-name?, cmp-field*, load-groups?, eager-load-group?, lazy-load-groups?, ! query*)> --- 499,505 ---- This includes both type mapping and function mapping. --> *************** *** 546,551 **** --- 557,577 ---- + + + + + + + + + + + + + + +