New day and a new issue with the SonarQube setup. Yesterday I documented the error and fix relating to an issue I faced while integrating SonarQube with the TFS Build.
Moving a step ahead, I encountered another issue, where the Begin Analysis Task was not able to complete processing.
The SonarQube Scanner for MSBuild – Begin Analysis task contacts the SonarQube server to retrieve the quality profile, and dynamically produces rulesets to be applied during the static analysis. It also sets things up so that the following MSBuild steps produce some data to prepare the analysis.
If there are issues in the process, I would recommend to go the Administration section in your SonarQube server, and navigate to the background Tasks tab.
Background Tasks page allows monitoring of the queue of tasks running asynchronously on the server. It also gives access to the history of finished tasks and their status. Analysis report processing is the most common kind of background task.
Click the ‘Show Error Details‘ button to view the error logs and you should be able to easily troubleshoot the root cause of the failure.
Looking at the error logs, I found a database error – String or binary data would be truncated.
This error was being thrown while inserting into the snapshots table in the SonarQube database. It is actually a very common error which is thrown when you try to insert any data in string (varchar,nvarchar,char,nchar) data type column which is more than size of the defined column in the table.
Caused by: org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated.### The error may involve org.sonar.db.component.SnapshotMapper.insert-Inline### The error occurred while setting parameters### SQL: insert into snapshots ( uuid, component_uuid, created_at, build_date, status, purge_status, islast, version, period1_mode, period1_param, period1_date) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated.
Looking at the table schema, the immediate thing which caught my attention was the bunch of period_mode and period_param columns which have size defined as NVARCHAR(100) – and might be candidates which can throw this error —
|CREATE TABLE [dbo].[snapshots](|
|[id] [INT] IDENTITY(1,1) NOT NULL,|
|[status] [NVARCHAR](4) NOT NULL,|
|[islast] [BIT] NOT NULL,|
|[version] [NVARCHAR](500) NULL,|
|[purge_status] [INT] NULL,|
|[period1_mode] [NVARCHAR](100) NULL,|
|[period1_param] [NVARCHAR](100) NULL,|
|[period2_mode] [NVARCHAR](100) NULL,|
|[period2_param] [NVARCHAR](100) NULL,|
|[period3_mode] [NVARCHAR](100) NULL,|
|[period3_param] [NVARCHAR](100) NULL,|
|[period4_mode] [NVARCHAR](100) NULL,|
|[period4_param] [NVARCHAR](100) NULL,|
|[period5_mode] [NVARCHAR](100) NULL,|
|[period5_param] [NVARCHAR](100) NULL,|
|[created_at] [BIGINT] NULL,|
|[build_date] [BIGINT] NULL,|
|[period1_date] [BIGINT] NULL,|
|[period2_date] [BIGINT] NULL,|
|[period3_date] [BIGINT] NULL,|
|[period4_date] [BIGINT] NULL,|
|[period5_date] [BIGINT] NULL,|
|[component_uuid] [NVARCHAR](50) NOT NULL,|
|[uuid] [NVARCHAR](50) NOT NULL,|
|CONSTRAINT [pk_snapshots] PRIMARY KEY CLUSTERED|
|)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]|
|) ON [PRIMARY]|
Using SQL Profiler, I recreated the issue and was able to confirm that the datatype on period1_param column was the culprit. I increased the size to NVARCHAR(500) on these columns and the data started getting saved successfully to the table.
This helped me to resolve the error – String or binary data would be truncated.
I will go ahead and report this issue to the SonarQube Team so that they can address this as part of their upcoming updates.
In case you have any other questions or face any other issues while installing SonarQube or upgrading it to a new version, please feel free to post a comment below and I would be happy to help.