Ant task to check if a database (connection) exists?

妖精的绣舞 提交于 2020-02-03 16:49:13

问题


is there a possibility in ANT to check whether a database (connection) exists or not without failing the build?

For example:

<target name="check-database-available">
    <sql
        classpath="${oracle.jar}" driver="oracle.jdbc.OracleDriver"
        url="jdbc:oracle:thin:@${my.db.host}:${my.db.port}:${my.db.sid}" 
        userid="${my.db.user}" 
        password="${my.db.pw}"
        onerror="continue" errorproperty="exit.status">
        select * from dual;
    </sql>
    <echo message="### exit status = ${exit.status}" />
</target>

This will always fail with BUILD FAILED and

java.sql.SQLException: ORA-01017: invalid username/password; logon denied

because the db does not exist yet. Setting "onerror" to "continue" and checking the "errorproperty" won't work as the task seems not to be executed.


回答1:


Starting with Ant v 1.8.0 you can use the failOnConnectionError attribute with the SQL Task.

The description reads as follows:

If false, will only print a warning message and not execute any statement if the task fails to connect to the database.

That looks like it would solve your problem.




回答2:


Here is a workaround which sets a db.present property (checkpresence.sql is a simple select statement)

<target name="check-db-presence">
  <echo message="Checking database presence at: ${db.url}"/>
  <delete file="tmp/db.present"/>
  <sql driver="${db.driver}" url="${db.url}"
                     userid="${db.userName}" password="${db.password}"
                     failOnConnectionError="false" onerror="continue" warningproperty="db.empty" errorproperty="db.empty" 
                      src="scripts/${db.platform}/checkpresence.sql"
                    print="true" output="tmp/db.present"/>
  <condition property="db.present">
    <available file="tmp/db.present"/>
  </condition>
</target>


来源:https://stackoverflow.com/questions/3352799/ant-task-to-check-if-a-database-connection-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!