Troubleshooting

The following section covers some common scenarios:

  • Anorm throws an error at the SqlMappingError runtime (too many rows when you're expecting a single one), even though the query resulted in expected behavior. It is an insert query using "on duplicate key update".

    This can happen when such a query is being executed using executeInsert. The executeInsert method should be used when we need to return an autogenerated key. If we are updating some fields through a duplicate key, it means that we do not actually need the key. We could use executeUpdate to add a check if one row has been updated. For example, we may want to update the wishlist table, which tracks what a user has wished for:

    DB.withConnection {
            implicit connection => {
    
      val updatedRows = SQL"""INSERT INTO wish_list (user_id, product_id, liked_at) VALUES ($userId,$productId,$likedAt)
        ON DUPLICATE KEY UPDATE liked_at=$likedAt, is_deleted=false """.executeUpdate()
    
      updatedRows == 1
      }
    }
  • Can we use multiple databases for a single application?

    Yes, it is possible to use a different database of the same as well as a different kind. If an application requires this, we can use two or more different relational or NoSQL databases or a combination of both. For example, the application may store its user data in SQL (as we already know the format of the user data) and the information about THE user's devices in MongoDB (since the devices are from different vendors, the format of their data can change).

  • Anorm does not throw a compilation error when a query has an incorrect syntax. Is there a configuration to enable this?

    It has been developed with the aim of using SQL queries in the code without any hassle. The developers are expected to pass correct queries to Anorm methods. To ensure that such errors do not occur at runtime, developers can execute the query locally and use it in the code if it succeeds. Alternatively, there are some third-party plugins that provide a typesafe DSL and can be used instead of Anorm if they meet the requirement, such as play-slick or scalikejdbc-play-support (https://github.com/scalikejdbc/scalikejdbc-play-support)

  • Is it possible to use another caching mechanism?

    Yes, it is possible to extend support for any other cache, such as OSCache, SwarmCache, MemCached, and so on, or a custom one by writing a plugin similar to EHCachePlugin. Some of the popular caching mechanisms already have Play plugins developed by individuals and/or other organizations. For example, play2-memcached (https://github.com/mumoshu/play2-memcached) and Redis plugin (https://github.com/typesafehub/play-plugins/tree/master/redis).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.170.187