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

  • Home
  • SEARCH
  • 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 7655223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:27:27+00:00 2026-05-31T12:27:27+00:00

I am trying to execute a SQL script in my JUnit test using Spring.

  • 0

I am trying to execute a SQL script in my JUnit test using Spring. The script is being used to set up data for the tests. However, when the script is run, the INSERTs in the script are being committed after each test. Spring documentation says not to expect rollback with DDL, but everything in my script is DML. All it contains is INSERT statements and obtaining the last insert ID (SET @blah = LAST_INSERT_ID()).

Am I configuring something wrong? I am using this against a MySQL database. Our configuration is as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
@TransactionConfiguration(defaultRollback = true)
public class OrderTestCase extends AbstractTransactionalJUnit4SpringContextTests {

    @Before
    public void runSql() {
        String fileName = StringUtils.replace(getClass().getName(), ".", "/") + ".sql";
        Resource resource = applicationContext.getResource(fileName);
        if (resource.exists()) {
            executeSqlScript(fileName, false);
        } else {
            LOGGER.debug("Resource doesn't exist: {}", resource);
        }
    }

@Test
public void testLoadOrders() {
    Collection<Order> orders= dao.findAll();
    assertTrue(orders.size() == 3);
}
}

Here is what I THINK is happening based on some research. The first call to executeSqlScript is running in a separate transaction. Spring’s SimpleJdbcTemplate.update method is called by executeSqlScript. Because this is a scoped to a JDBC connection, which is obtained from a connection pool, I am not guaranteed to get the same connection on subsequent accesses to the DB and therefore can’t be guaranteed to run in the same transaction.

If I were to do all my DB operations through the TransactionManager or (Hibernate Session Factory) then it would work because of how the internals scope the transactions. My options here are to:

  1. Figure out how to run SimpleJdbcTemplate.update and the subsequent actual code I’m testing in the same transaction. I think I can possibly do this, but my efforts so far have been fruitless.

  2. Do all my test data set up through the SessionFactory. So, instead of executing straight SQL scripts through JDBC, I would be populating model objects and persisting them through a Hibernate DAO.

Am I on the right track here? Can anyone provide any more guidance?

  • 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-31T12:27:29+00:00Added an answer on May 31, 2026 at 12:27 pm

    I was able to solve this by adding the following ‘jpaVendorAdapter’ to the declaration of my entityManagerFactory.

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
            </property>
    </bean>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to execute a sql script using the task in SSIS. My script
Hello I have a very large SQL script and while trying to execute it
Below you could see template of SQL script that i'm trying to execute on
I'm trying to programmatically add an Execute SQL task from within a script task
I am trying to execute dynamic SQL in a Servlet using JDBC, I need
I'm trying to execute the following line: exit | sqlplus username/password@sid @test.sql Works great
I'm getting a timeout error when trying to execute a LINQ (-to-SQL) query System.Data.SqlClient.SqlException:
I'm trying to load up rails in the test environment using a ruby script.
I'm trying to write a powershell script which will execute sqlcmd.exe to run a
When I am trying to execute my sql script by Flyway, I get java.sql.SQLSyntaxErrorException:

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.