Help us understand the problem. What is going on with this article?

【Scala】AnormでStackOverflowErrorと言われた時の対処法

More than 1 year has passed since last update.

TL; DR

AnormでSQL文を実行する際のSQL文が長すぎるとStackOverflowErrorがthrowされる。
解決方法はクエリの長い部分を別途Stringで切り出すことで回避可能

内容

AnormでSQL文を実行する下記関数

val sql = "INSERT *******"
anorm.SQL(sql).execute()
anorm.SQL(sql).executeInsert()
anorm.SQL(sql).executeUpdate()

において、sql: Stringが長すぎると、StackOverflowErrorとなる。
(具体的に何文字かは不明だが、自分は約5000文字の場合エラーとなった)

回避方法として、下記のようにクエリの一部分を生文字リテラルとして切り出してしまうとうまくいく。

val text = """(めちゃめちゃ長いstring)"""
val sql = s"""INSERT INTO xxx (id, text) VALUES ("1", "$text");"""
SQL(sql).executeInsert()

補足

ちなみにslickの

Q.updateNA(xxx).execute

でも問題なく実行できます。自分は結局こっちを使いました(約5000文字でも問題なく実行できた)

何かご指摘やもっと良い方法等がありましたらコメントお願いします!

asa_s
Unity Engineer (2016 - now), Scala Engineer (2016 - now), iOS Engineer (2014 - 2016)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした