DayTrader
  1. DayTrader
  2. DAYTRADER-14

Include sql script in the ear and use a gbean to create tables etc

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.2
    • Fix Version/s: 1.2, 2.0
    • Component/s: EJB Tier
    • Labels:
      None

      Description

      You can use the DatabaseIntitializationGBean (GERONIMO-2396) in a g. plan and include the sql script in the ejb module so the database will get created if not already present. This is way better than the previous hack of including a pre-built database in the car file.

      1. DAYTRADER-14.patch
        3 kB
        David Jencks
      2. daytrader-14.patch.1019
        32 kB
        Piyush Agarwal
      3. daytrader-14.patch.11152006
        32 kB
        Piyush Agarwal
      4. daytrader-14.patch.11202006
        23 kB
        Piyush Agarwal
      5. daytrader-14.patch.11202006
        24 kB
        Piyush Agarwal
      6. d-j-plan.xml
        34 kB
        David Jencks

        Issue Links

          Activity

          Hide
          David Jencks added a comment -

          DAYTRADER-14.patch adds the sql script to the ejb module.

          d-j-plan.xml is a plan that works for both g-jetty and g-tomcat that uses the DatabaseInitiializationGBean to create the tables and deploys both the app clients. Works on trunk for me.

          Show
          David Jencks added a comment - DAYTRADER-14 .patch adds the sql script to the ejb module. d-j-plan.xml is a plan that works for both g-jetty and g-tomcat that uses the DatabaseInitiializationGBean to create the tables and deploys both the app clients. Works on trunk for me.
          Hide
          Christopher James Blythe added a comment -

          David... this sounds great for use on Geronimo. However, we will still need a method for creating the database tables and indexes on other app servers like JBoss, WebLogic, WebSphere, etc.

          As a side note, the commands needed to create the tables and indexes are SQL statements themselves and could be executed within the application as JDBC statements. Just brain storming here... we could hook into the buildDB servlet action which populates the database or we could provide another link on the config page to execute the code to create the tables. Once the database is created and we have sufficeint access rights, we should be good to go. The only obsticle I can think of would be if there are any specifics that we need to account for in the schemas for various database vendors (ie. Derby, Oracle, DB2, etc.).

          Thoughts???

          Show
          Christopher James Blythe added a comment - David... this sounds great for use on Geronimo. However, we will still need a method for creating the database tables and indexes on other app servers like JBoss, WebLogic, WebSphere, etc. As a side note, the commands needed to create the tables and indexes are SQL statements themselves and could be executed within the application as JDBC statements. Just brain storming here... we could hook into the buildDB servlet action which populates the database or we could provide another link on the config page to execute the code to create the tables. Once the database is created and we have sufficeint access rights, we should be good to go. The only obsticle I can think of would be if there are any specifics that we need to account for in the schemas for various database vendors (ie. Derby, Oracle, DB2, etc.). Thoughts???
          Hide
          Matt Hogstrom added a comment -

          Chris,

          I started looking at that option. Of course we'll need to connect to the DataSource and check which DB vendor we're connecting to. Based on that we could simply keep a set of external files we could read and execute. That would be pretty helpful.

          Unfortunately, the database still needs to be created. Not sure if there is a way around that .

          Show
          Matt Hogstrom added a comment - Chris, I started looking at that option. Of course we'll need to connect to the DataSource and check which DB vendor we're connecting to. Based on that we could simply keep a set of external files we could read and execute. That would be pretty helpful. Unfortunately, the database still needs to be created. Not sure if there is a way around that .
          Hide
          Piyush Agarwal added a comment -

          Matt, Chris, David
          DatabaseMetaData is implemented by JDBC drivers and allows you to check which DB you are working against. Based on that db-specific SQL statements for dropping, creating the tables, indexes etc can be executed. Creating the Database via JDBC is not supported by most databases and so that will still need to be created by hand.

          In spite of the above tiny shortcoming, this will make the Daytrader app a lot more self-contained and still open to work on all the different app servers.

          Show
          Piyush Agarwal added a comment - Matt, Chris, David DatabaseMetaData is implemented by JDBC drivers and allows you to check which DB you are working against. Based on that db-specific SQL statements for dropping, creating the tables, indexes etc can be executed. Creating the Database via JDBC is not supported by most databases and so that will still need to be created by hand. In spite of the above tiny shortcoming, this will make the Daytrader app a lot more self-contained and still open to work on all the different app servers.
          Hide
          Piyush Agarwal added a comment -

          After looking at the code I think we can lookup the database product name using DatabaseMetaData and based on that read a specific DDL file to drop and recreate the db tables. This will account for the differences in schemas of the various database vendors like Derby, db2, oracle etc. It will also save us from hard-coding those DDL-SQL in the application and having a nightmare everytime we change/update the database schema.

          The DDL files will be packaged at a specific location within the Daytrader web.war file (say something like web.war/WEB-INF/dbscripts/ folder). Right now we can detect and provide DDL files for databases like Db2, derby and oracle ("tested" databases); for any other "un-tested" database we can make a provision for the user to supply the DDL at a location like web.war/WEB-INF/dbscripts/OTHER/Table.ddl and it will be executed.

          Show
          Piyush Agarwal added a comment - After looking at the code I think we can lookup the database product name using DatabaseMetaData and based on that read a specific DDL file to drop and recreate the db tables. This will account for the differences in schemas of the various database vendors like Derby, db2, oracle etc. It will also save us from hard-coding those DDL-SQL in the application and having a nightmare everytime we change/update the database schema. The DDL files will be packaged at a specific location within the Daytrader web.war file (say something like web.war/WEB-INF/dbscripts/ folder). Right now we can detect and provide DDL files for databases like Db2, derby and oracle ("tested" databases); for any other "un-tested" database we can make a provision for the user to supply the DDL at a location like web.war/WEB-INF/dbscripts/OTHER/Table.ddl and it will be executed.
          Hide
          Piyush Agarwal added a comment -

          I almost have the entire changes done which are required to make the above work. On testing the drop tables and repopulate against Daytrader I came across a problem in repopulation part which was causing DuplicateKeyException to be thrown. On analyzing I realized that the repopulate db part which my code was calling after recreating the tables never deleted/repopulated the KeyGen table. The KeySequenceDirect and KeySequenceBean cache blocks of Ids from this keygen table and use them for holdings, orders etc. When the table is deleted, the caches dont realize this and continue allocating ids from the cache until they run-out and go to the table to get new block of ids. Since the table is re-created, the ids which were used before from the cache are re-generated causing DuplicateKeyExceptions.

          To solve this I thought of two options -
          1) Do not drop the keyGen table in my ddl script. The attempt to create the KeyGen table will fail with SQLException in case it already exists. This will cause the repopulate code to work as-is and prevent the DuplicateKeyException.
          2) The cleaner way (avoiding all exceptions) will be to signal the KeySequenceDirect and KeySequenceBean (depending on Direct or EJB mode) to drop their cached blocks when the repopulation of the tables is being done. This will require some code to be added to the above classes but I think its still do-able without a lot of new code. Also this will be the right way of doing it.

          So currently I am going to evaluate the Option 2 unless anyone has something better in mind.

          Show
          Piyush Agarwal added a comment - I almost have the entire changes done which are required to make the above work. On testing the drop tables and repopulate against Daytrader I came across a problem in repopulation part which was causing DuplicateKeyException to be thrown. On analyzing I realized that the repopulate db part which my code was calling after recreating the tables never deleted/repopulated the KeyGen table. The KeySequenceDirect and KeySequenceBean cache blocks of Ids from this keygen table and use them for holdings, orders etc. When the table is deleted, the caches dont realize this and continue allocating ids from the cache until they run-out and go to the table to get new block of ids. Since the table is re-created, the ids which were used before from the cache are re-generated causing DuplicateKeyExceptions. To solve this I thought of two options - 1) Do not drop the keyGen table in my ddl script. The attempt to create the KeyGen table will fail with SQLException in case it already exists. This will cause the repopulate code to work as-is and prevent the DuplicateKeyException. 2) The cleaner way (avoiding all exceptions) will be to signal the KeySequenceDirect and KeySequenceBean (depending on Direct or EJB mode) to drop their cached blocks when the repopulation of the tables is being done. This will require some code to be added to the above classes but I think its still do-able without a lot of new code. Also this will be the right way of doing it. So currently I am going to evaluate the Option 2 unless anyone has something better in mind.
          Hide
          Matt Hogstrom added a comment -

          The challenge as you pointed out is to somehow invalidate the KeySequence SLSBs. Since their most likely pooled it makes it a bit more tricky. The simplest way to do this which is non-standard would be to use a static int or some other fingerprint on the integrity of the tables. However, this doesn't help the situation when your running a cluster of the application so its applicability would be really to a single server. One possibility is to put in a timestamp as static that indicates the last time the table was valid. Then, for each SLSB keep a last used time and compare the two. If the last used is less than the valid then the beans need to be refreshed so all values could be reset at that time.

          The easiest solution I think is to allow the tables to be created...if they already exist warn the user that they need to recycle their environment. I'd be happy with that solution as its way better than what we have. If someone is repopulating...they should be fine with recycling. If possible, I'd add some text when a duplicate key exception occurs that would tip off the user of the possible problem with the Key entity.

          Show
          Matt Hogstrom added a comment - The challenge as you pointed out is to somehow invalidate the KeySequence SLSBs. Since their most likely pooled it makes it a bit more tricky. The simplest way to do this which is non-standard would be to use a static int or some other fingerprint on the integrity of the tables. However, this doesn't help the situation when your running a cluster of the application so its applicability would be really to a single server. One possibility is to put in a timestamp as static that indicates the last time the table was valid. Then, for each SLSB keep a last used time and compare the two. If the last used is less than the valid then the beans need to be refreshed so all values could be reset at that time. The easiest solution I think is to allow the tables to be created...if they already exist warn the user that they need to recycle their environment. I'd be happy with that solution as its way better than what we have. If someone is repopulating...they should be fine with recycling. If possible, I'd add some text when a duplicate key exception occurs that would tip off the user of the possible problem with the Key entity.
          Hide
          Piyush Agarwal added a comment -

          Matt, thanks for the headsup and valuable insight. So inorder to keep things simple and not have to change the logic inside the KeySequnceBeans and also avoid catching the DuplicateKeyExceptions I propose doing the following – Drop and Recreate the tables, at end of that the user will get a prominent message to Stop and Restart their application and then use the link to Repopulate the daytrader database.

          Since the user will be recycling his environment before the repopulation of the tables the SLSBs will get recycled and the DuplicateKeyExceptions will be avoided.

          Thoughts??

          Show
          Piyush Agarwal added a comment - Matt, thanks for the headsup and valuable insight. So inorder to keep things simple and not have to change the logic inside the KeySequnceBeans and also avoid catching the DuplicateKeyExceptions I propose doing the following – Drop and Recreate the tables, at end of that the user will get a prominent message to Stop and Restart their application and then use the link to Repopulate the daytrader database. Since the user will be recycling his environment before the repopulation of the tables the SLSBs will get recycled and the DuplicateKeyExceptions will be avoided. Thoughts??
          Hide
          Matt Hogstrom added a comment -

          I'm open to other ideas but I think that is clear and makes life the easiest.

          Show
          Matt Hogstrom added a comment - I'm open to other ideas but I think that is clear and makes life the easiest.
          Hide
          Piyush Agarwal added a comment -

          Attaching the patch which will allow the creation of Daytrader database tables from within Daytrader configuration panels. The patch updates the Configuration panel to include a new link "Recreate Daytrader tables and indexes" which will automatically detect Derby, Db2 and Oracle databases and use the ddl that I have provided for those databases to drop and recreate the database. any other unsupported database ddl can be placed under <AppDeploypath>/web.war/dbscripts/other/Table.ddl and it will be picked up and used. Users will need to stop the Application and restart it after doing this step and then use the "Repopulate Daytrader Database" link to populate the newly created tables. The Stop and restart is needed to flush the cached ids from KeySequenceDirect and KeySequenceBean and prevent DuplicateKeyExceptions (refer to Daytrader-14 JIRA comments for more details).

          With the implementation of this feature the createDB.sh script provided under the modules/derby can be changed to just create the empty database and no longer needs to run the modules/derby/src/sql/daytrader.sql to establish the tables. The README should be changed to instruct the users to use this new feature to establish their daytrader database tables. But I will leave that as an open item to be discussed and taken care of later.

          Show
          Piyush Agarwal added a comment - Attaching the patch which will allow the creation of Daytrader database tables from within Daytrader configuration panels. The patch updates the Configuration panel to include a new link "Recreate Daytrader tables and indexes" which will automatically detect Derby, Db2 and Oracle databases and use the ddl that I have provided for those databases to drop and recreate the database. any other unsupported database ddl can be placed under <AppDeploypath>/web.war/dbscripts/other/Table.ddl and it will be picked up and used. Users will need to stop the Application and restart it after doing this step and then use the "Repopulate Daytrader Database" link to populate the newly created tables. The Stop and restart is needed to flush the cached ids from KeySequenceDirect and KeySequenceBean and prevent DuplicateKeyExceptions (refer to Daytrader-14 JIRA comments for more details). With the implementation of this feature the createDB.sh script provided under the modules/derby can be changed to just create the empty database and no longer needs to run the modules/derby/src/sql/daytrader.sql to establish the tables. The README should be changed to instruct the users to use this new feature to establish their daytrader database tables. But I will leave that as an open item to be discussed and taken care of later.
          Hide
          Christopher James Blythe added a comment -

          Piyush...

          I really like what you have here... It's clean, simple, and makes working with the default derby database so much easier. We no long have to run and external sql script or provide a pre-built derby database.

          Actually, I went ahead and changed the deployment plans in Daytrader-24 to create the derby db by default.

          Just a few more comments...

          • I like how feedback is written to the page while created the db. How do you feel about writing out the status for each sql command that is executed instead of waiting for all of them to complete. If for some reason, one were to fail it would be nice to see it in the browser screen. That way we don't have to dig through the logs to see what happened.
          • Looks like there might be a type of two in your trace/error statements... In TradeDirect.java - "thrown on executing the [foll] sql command:"
          • We need to sync up the ddl files - specifically, the indexes. I will create another JIRA to address this.

          Chris

          Show
          Christopher James Blythe added a comment - Piyush... I really like what you have here... It's clean, simple, and makes working with the default derby database so much easier. We no long have to run and external sql script or provide a pre-built derby database. Actually, I went ahead and changed the deployment plans in Daytrader-24 to create the derby db by default. Just a few more comments... I like how feedback is written to the page while created the db. How do you feel about writing out the status for each sql command that is executed instead of waiting for all of them to complete. If for some reason, one were to fail it would be nice to see it in the browser screen. That way we don't have to dig through the logs to see what happened. Looks like there might be a type of two in your trace/error statements... In TradeDirect.java - "thrown on executing the [foll] sql command:" We need to sync up the ddl files - specifically, the indexes. I will create another JIRA to address this. Chris
          Hide
          Piyush Agarwal added a comment -

          Chris, Glad you like the new feature!

          I had originally coded to have the SQL exceptions write out to both the browser and the logs, but later I went ahead and disabled the output to the browser output. There were two reasons -
          1) There are some SQL statements that we expect to fail. for eg - the scripts have Drop tables commands at the start and then the create table commands. The first time the daytrader db is created all the Drop table commands will fail as no tables exist. I didnt want to write out these exceptions to the browser and confuse the user.
          2) In niost cases the SQL Exception is quite verbose and if you have couple of them happen... they can quickly fill up the browser screen completely. So imagine the drop commands above failing and your screen getting flooded with SQL exceptions, plus this is bound to happen on every first install of the Daytrader app and creation of the database.

          But it does give me an idea. How about I write out the failed SQL command to the browser screen and mention to the user to look up this SQL command and its associated SQL exception in the logs. This way the user will exactly know which SQL command failed, if its one of the SQL commands which shouldn't have failed they can look up the Exception in the logs, if its one of the commands which can generally fail they can safely ignore the message.

          Show
          Piyush Agarwal added a comment - Chris, Glad you like the new feature! I had originally coded to have the SQL exceptions write out to both the browser and the logs, but later I went ahead and disabled the output to the browser output. There were two reasons - 1) There are some SQL statements that we expect to fail. for eg - the scripts have Drop tables commands at the start and then the create table commands. The first time the daytrader db is created all the Drop table commands will fail as no tables exist. I didnt want to write out these exceptions to the browser and confuse the user. 2) In niost cases the SQL Exception is quite verbose and if you have couple of them happen... they can quickly fill up the browser screen completely. So imagine the drop commands above failing and your screen getting flooded with SQL exceptions, plus this is bound to happen on every first install of the Daytrader app and creation of the database. But it does give me an idea. How about I write out the failed SQL command to the browser screen and mention to the user to look up this SQL command and its associated SQL exception in the logs. This way the user will exactly know which SQL command failed, if its one of the SQL commands which shouldn't have failed they can look up the Exception in the logs, if its one of the commands which can generally fail they can safely ignore the message.
          Hide
          Christopher James Blythe added a comment -

          To quote the Guinness commercials... "Brilliant"!

          Show
          Christopher James Blythe added a comment - To quote the Guinness commercials... "Brilliant"!
          Hide
          Piyush Agarwal added a comment -

          Based on the last comments by Chris, I have added the facility for failed SQL statements to be written out on the browser screen. The user is instructed to look up the exception details related to this SQL failure in the log.

          Show
          Piyush Agarwal added a comment - Based on the last comments by Chris, I have added the facility for failed SQL statements to be written out on the browser screen. The user is instructed to look up the exception details related to this SQL failure in the log.
          Hide
          Piyush Agarwal added a comment -

          Just figured out that due to some error with the TortoiseSVN's patch creation my last patch has the ddl files included twice.
          I have fixed this and uploading the patch again. So please ignore the previous files and use the file daytrader-14.patch.11202006

          Show
          Piyush Agarwal added a comment - Just figured out that due to some error with the TortoiseSVN's patch creation my last patch has the ddl files included twice. I have fixed this and uploading the patch again. So please ignore the previous files and use the file daytrader-14.patch.11202006
          Hide
          Piyush Agarwal added a comment -

          Left in an extra print statement which was misleading, now removed.

          Show
          Piyush Agarwal added a comment - Left in an extra print statement which was misleading, now removed.
          Hide
          Piyush Agarwal added a comment -

          Note to committers- Latest patch is daytrader-14.patch.11202006, 20/Nov/06 02:27 PM (23KB)

          Show
          Piyush Agarwal added a comment - Note to committers- Latest patch is daytrader-14.patch.11202006, 20/Nov/06 02:27 PM (23KB)
          Hide
          Matt Hogstrom added a comment -

          trunk/
          Sending trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java
          Sending trunk/modules/web/src/main/java/org/apache/geronimo/samples/daytrader/web/TradeBuildDB.java
          Sending trunk/modules/web/src/main/java/org/apache/geronimo/samples/daytrader/web/TradeConfigServlet.java
          Sending trunk/modules/web/src/main/webapp/configure.html
          Adding trunk/modules/web/src/main/webapp/dbscripts
          Adding trunk/modules/web/src/main/webapp/dbscripts/db2
          Adding trunk/modules/web/src/main/webapp/dbscripts/db2/Table.ddl
          Adding trunk/modules/web/src/main/webapp/dbscripts/derby
          Adding trunk/modules/web/src/main/webapp/dbscripts/derby/Table.ddl
          Adding trunk/modules/web/src/main/webapp/dbscripts/oracle
          Adding trunk/modules/web/src/main/webapp/dbscripts/oracle/Table.ddl
          Transmitting file data .......
          Committed revision 482366.

          branches/1.2
          Sending 1.2/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java
          Sending 1.2/modules/web/src/main/java/org/apache/geronimo/samples/daytrader/web/TradeBuildDB.java
          Sending 1.2/modules/web/src/main/java/org/apache/geronimo/samples/daytrader/web/TradeConfigServlet.java
          Sending 1.2/modules/web/src/main/webapp/configure.html
          Adding 1.2/modules/web/src/main/webapp/dbscripts
          Adding 1.2/modules/web/src/main/webapp/dbscripts/db2
          Adding 1.2/modules/web/src/main/webapp/dbscripts/db2/Table.ddl
          Adding 1.2/modules/web/src/main/webapp/dbscripts/derby
          Adding 1.2/modules/web/src/main/webapp/dbscripts/derby/Table.ddl
          Adding 1.2/modules/web/src/main/webapp/dbscripts/oracle
          Adding 1.2/modules/web/src/main/webapp/dbscripts/oracle/Table.ddl
          Transmitting file data .......
          Committed revision 482359.

          Show
          Matt Hogstrom added a comment - trunk/ Sending trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java Sending trunk/modules/web/src/main/java/org/apache/geronimo/samples/daytrader/web/TradeBuildDB.java Sending trunk/modules/web/src/main/java/org/apache/geronimo/samples/daytrader/web/TradeConfigServlet.java Sending trunk/modules/web/src/main/webapp/configure.html Adding trunk/modules/web/src/main/webapp/dbscripts Adding trunk/modules/web/src/main/webapp/dbscripts/db2 Adding trunk/modules/web/src/main/webapp/dbscripts/db2/Table.ddl Adding trunk/modules/web/src/main/webapp/dbscripts/derby Adding trunk/modules/web/src/main/webapp/dbscripts/derby/Table.ddl Adding trunk/modules/web/src/main/webapp/dbscripts/oracle Adding trunk/modules/web/src/main/webapp/dbscripts/oracle/Table.ddl Transmitting file data ....... Committed revision 482366. branches/1.2 Sending 1.2/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java Sending 1.2/modules/web/src/main/java/org/apache/geronimo/samples/daytrader/web/TradeBuildDB.java Sending 1.2/modules/web/src/main/java/org/apache/geronimo/samples/daytrader/web/TradeConfigServlet.java Sending 1.2/modules/web/src/main/webapp/configure.html Adding 1.2/modules/web/src/main/webapp/dbscripts Adding 1.2/modules/web/src/main/webapp/dbscripts/db2 Adding 1.2/modules/web/src/main/webapp/dbscripts/db2/Table.ddl Adding 1.2/modules/web/src/main/webapp/dbscripts/derby Adding 1.2/modules/web/src/main/webapp/dbscripts/derby/Table.ddl Adding 1.2/modules/web/src/main/webapp/dbscripts/oracle Adding 1.2/modules/web/src/main/webapp/dbscripts/oracle/Table.ddl Transmitting file data ....... Committed revision 482359.

            People

            • Assignee:
              Matt Hogstrom
              Reporter:
              David Jencks
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development