You can diagnose and prevent database save failures caused by text exceeding a String attribute's configured maximum length; this page is for MDriven Designer and MDrivenServer developers who write text through OCL or EAL.
Recognize the error
When MDrivenServer saves an object, the database rejects a value that is longer than the destination String column. The server log can contain an error such as:
String or binary data would be truncated
The error details can identify the value that failed. For example:
@errorinfo=S_OutgoingEmail.SS_OutgoingEma... (length=1153) (System.String) (DbType: String)
In this example, a server-side email action received an exception and attempted to save the exception text in ErrorInfo. The text was 1153 characters long, but the ErrorInfo attribute did not allow that many characters.
Find the attribute and the value being written
- Read the log entry to identify the attribute or value named in the error details.
- Find the code path that assigns the text. Check OCL expressions, EAL assignments, and server-side actions that construct messages.
- Check the destination String attribute's configured maximum length in the model. A String attribute is normally persistent and saved to the database; see defining an attribute.
- Compare the produced text with the destination limit. Error messages, concatenated text, and text received from another system can be much longer than expected.
Do not treat the database error as validation. The save has already failed. Ensure that the model can store the information you need, and restrict values before they are assigned when their length is not predictable.
Choose how to handle long text
Use one or both of the following measures.
| Measure | Use it when | Example |
|---|---|---|
| Increase the String attribute length | The complete text is meaningful and must be retained. | Increase the length of ErrorInfo when full exception messages must be stored.
|
| Truncate the value before assignment | The incoming text can vary in length and the destination has a fixed limit. | Store the beginning of an external response in a limited-length status attribute. |
Increasing the attribute length preserves more data, but it does not protect a fixed-length attribute from an unexpectedly longer future value. Truncation prevents the save failure, but it discards the part of the text beyond the chosen limit. Apply both when you need a larger stored message while still protecting the save operation.
Truncate a value with OCL
maxLength returns the maximum length allowed for a String attribute. Use it with subString to limit text to the target attribute's capacity.
<attribute>.subString(1,<attribute>.maxLength-1)
For the ErrorInfo example, the truncation expression is:
self.errorinfo.subString(1,self.errorinfo.maxLength-1)
Use the expression on the text that will be written, before the assignment. For example, when an expression produces an error message for ErrorInfo, limit that produced text using self.errorinfo.maxLength as the destination limit.
self.errorinfo := <error message expression>.subString(1,self.errorinfo.maxLength-1)
Replace <error message expression> with the expression that produces the message. The important point is that the value is shortened before it is persisted, while the limit is taken from the attribute that will store it.
Use the right place for the check
Place truncation at the boundary where variable-length text enters a limited String attribute. Common boundaries include:
- An EAL assignment that stores a generated message.
- An OCL expression that prepares text for a String attribute.
- Handling of text returned by another system.
- A derived settable attribute that accepts user input. For how EAL handles user input through
vInputParameter, see Documentation:Derived settable attributes.
For example, if a user can enter a long note into a derived settable String attribute that ultimately writes to a shorter persistent attribute, limit the entered value as part of the EAL that performs the assignment. Do not wait until the database save.
Verify the fix
- Test with text shorter than the attribute limit and confirm that the complete value is saved.
- Test with text longer than the limit and confirm that the object saves without the truncation error.
- Confirm whether the retained portion of the text is sufficient for diagnosis or user-facing information.
- If the omitted text is required, increase the destination attribute length and repeat the test.
