Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

java - Lock oracle database before running the Delete/Load data scripts

we are running Junit ans Selenium test cases from CI every midnight. We are pre populating the data using the Maven-SQL plugin as following.

          <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>create-database-tables</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <autocommit>false</autocommit>
                            <onError>continue</onError>
                            <srcFiles>
                                <srcFile>../sql/delete_data.sql</srcFile>
                                <srcFile>../sql/load_data.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

we are frequently facing the DB deadlocks due to simultaneous builds by different users. The solution we thought is to lock the database before running the DB scripts.

Can we lock the DB access before running the scripts and unlock it after running the scripts.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A shared database for testing is never a great idea, presumably you know this which is why you're asking how to restrict access to one user at a time.

Preaching aside..... I'd like to offer a left-field solution of liquibase to manage both the database schema and data population. Has lots of useful features one of which is that it will automatically lock the database and prevent two instance of liquibase interfering with each other.

Example

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myspotontheweb.db</groupId>
    <artifactId>liquibase-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <!-- Liquibase settings -->
        <liquibase.url>jdbc:h2:target/db1/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
        <liquibase.driver>org.h2.Driver</liquibase.driver>
        <liquibase.username>user</liquibase.username>
        <liquibase.password>pass</liquibase.password>
        <liquibase.changeLogFile>com/myspotontheweb/db/changelog/db-changelog-master.xml</liquibase.changeLogFile>
        <liquibase.promptOnNonLocalDatabase>false</liquibase.promptOnNonLocalDatabase>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.162</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>2.0.2</version>
                <executions>
                    <execution>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>update</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...