Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 920777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:46:26+00:00 2026-05-15T18:46:26+00:00

I know that SchemaExport should be my friend. But I am using liquibase and

  • 0

I know that SchemaExport should be my friend. But I am using liquibase and want to execute the DDL – pure sql statements – generated from liquibase to recreate the database before every test method.

Do you see problems with the following code? I wonder that this seems to be so complicated …

public static int executeScript(String sqlFileOnClasspath) {
    Session sess = getSessionFactory().openSession();
    Transaction ta = sess.beginTransaction();
    int sqlCmd = 0;

    try {
        BufferedReader bReader = new BufferedReader(new InputStreamReader(
                  Util.class.getResourceAsStream(sqlFileOnClasspath), "UTF-8"));
        String line;
        while ((line = bReader.readLine()) != null) {
            if (line.startsWith("--") || line.trim().isEmpty())
                continue;

            final String tmp = line;
            sess.doWork(new Work() {

                @Override
                public void execute(Connection connection) throws SQLException {
                    connection.createStatement().execute(tmp);
                }
            });
            sqlCmd++;
        }
    } catch (Exception ex) {
        log.error("Couldn't execute " + sqlFileOnClasspath, ex);
    } finally {
        ta.commit();
        sess.close();
    }

    return sqlCmd;
}

BTW: For liquibase you will need to do:

    // remove all hibernate managed tables
    SchemaExport schemaTool = new SchemaExport(getConfiguration()); 
    schemaTool.drop(false, true);

    // DROP TABLE DATABASECHANGELOGLOCK;
    // DROP TABLE DATABASECHANGELOG;
    executeScript("/drop-none-hib.sql");

    // now execute the DDL statements from liquibase
    int sqlCmd = executeScript("/schema.sql");
    log.info("Executed " + sqlCmd + " sql commands");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:46:27+00:00Added an answer on May 15, 2026 at 6:46 pm

    Security

    Directly executing unprepared statements may lead to SQL injection. However, you seem to read DDL statements from a static file, so this shouldn’t be a problem, just wanted to note that.

    Exception Handling

    Missing entirely, e.g. properly closing the input streams, the connections etc. Also, it misses having detailed information on which of the statements failed if any of the SQLExceptions are being thrown. sqlCmd variable also counts failed statements, so I doubt the usefulness of this counter.

    Parsing SQL/DDL

    You’re testing for -- style comments, but not for /* */ comments. That’ll bite you some day. Also, the parser assumes a SQL statement per line. Longer statements may be multiline and end with semicolons which need to be trimmed. Not sure about how liquibase generates the statements though, so this may not be a real problem.

    Transaction Handling

    DDL statements cannot be rolled back anyway, so not sure about the transactions being helpful. Please correct me if i’m wrong.

    Otherwise, as it’s for testing, i looks fine to me too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that Java have its own garbage collection, but sometimes I want to
I know that someone is going to want code for this issue but the
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that in order to download a cookie associated with an account using
I know that design patterns is generally something that's connected to OO programming, but
I know that there are more than a dozen questions about this. But I
I know that ads colors can be set up in account settings, but I
I know that there's something fishy about the malloc part, but I'm having trouble
I know that Apple's terms for apps disallows downloading and executing code, but there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.