Fix deadlock by querying only the procedure created
*/
package com.microsoft.sqlserver.jdbc.connection;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
@@ -29,14 +31,14 @@ public class DBMetadataTest extends AbstractTest {
@Test
public void testDatabaseMetaData() throws SQLException {
String functionName = RandomUtil.getIdentifier("proc");
- functionName = DBTable.escapeIdentifier(functionName);
+ String escapedFunctionName = DBTable.escapeIdentifier(functionName);
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
String sqlDropFunction = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo]."
- + TestUtils.escapeSingleQuotes(functionName) + "')" + "and xtype in (N'FN', N'IF', N'TF'))"
- + "drop function " + functionName;
- String sqlCreateFunction = "CREATE FUNCTION " + functionName
+ + TestUtils.escapeSingleQuotes(escapedFunctionName) + "')" + "and xtype in (N'FN', N'IF', N'TF'))"
+ + "drop function " + escapedFunctionName;
+ String sqlCreateFunction = "CREATE FUNCTION " + escapedFunctionName
+ " (@text varchar(8000), @delimiter varchar(20) = ' ') RETURNS @Strings TABLE "
+ "(position int IDENTITY PRIMARY KEY, value varchar(8000)) AS BEGIN INSERT INTO @Strings VALUES ('DDD') RETURN END ";
@@ -47,11 +49,11 @@ public class DBMetadataTest extends AbstractTest {
stmt.execute(sqlCreateFunction);
DatabaseMetaData md = con.getMetaData();
- try (ResultSet arguments = md.getProcedureColumns(null, null, null, "@TABLE_RETURN_VALUE")) {
-
+ try (ResultSet arguments = md.getProcedureColumns(null, null, functionName, "@TABLE_RETURN_VALUE")) {
if (arguments.next()) {
- arguments.getString("COLUMN_NAME");
- arguments.getString("DATA_TYPE"); // call this function to make sure it does not crash
+ assertTrue(arguments.getString("PROCEDURE_NAME").startsWith(functionName), "Expected Stored Procedure was not retrieved.");
+ } else {
+ assertTrue(false, "Expected Stored Procedure was not found.");
}
arguments.getStatement().close();
}
