Related: #17907

Affected code:

https://github.com/spring-projects/spring-framework/blob/04eed155cdf10dd943818c343c9fd49fb6d8fcf4/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java#L71

Question:

Order of execution of SQL statement if often extremely important. The larger the test base and the number of programmers, the more confusion may be created.

@Sql annotation already supports String[] value attribute, which means the implementor can input the SQL scritps to run in their desired order.

I haven't found relevant documentation yet, but I remember from the old times of Java when there was no explicit guarantee on reflection ordering. Ie. earlier JVMs and in general any third-party JVM can decide the reflection's order of scanning, at least with class members. This results in defining class members in a certain order in the source file, but enumerating them via reflection in another order.

Arrays are ordered by design, so using @Sql with an explciit array works as designed everywhere.

I wouldn't bet that repeated @Sql annotations always yield the desired order of execution no matter the JVM.

Comment From: sbrannen

Do we really need @Sql to be repeatable?

Yes. Without that support one could not declare different configuration for SQL scripts executed at the same level, which is a core feature.

In addition, without that support one could not declare @Sql on a composed annotation for reuse.

I wouldn't bet that repeated @Sql annotations always yield the desired order of execution no matter the JVM.

They are actually guaranteed to be returned in declaration order.

From the JLS § 9.7.5:

The elements of the (array-typed) value element of the container annotation are all the base annotations in the left-to-right order in which they appeared in the context.

"left-to-right order" means the same thing as "top down", since you normally declare each annotation on a separate line.

In light of that, I am closing this issue.