Kyle Valade

near-sighted things

Read this first

Getting SQL Server to work with Play Framework

I’m using the jTDS driver with Play 2.5.8 (which uses HikariCP) and kept getting exceptions like the following when trying to create connection pools on startup.

[info] application - Creating Pool for datasource 'default'
[error] c.z.h.p.PoolBase - HikariPool-1 - Failed to execute isValid() for connection, configure connection test query. (null)
[info] application - Creating Pool for datasource 'default'
[error] c.z.h.p.PoolBase - HikariPool-2 - Failed to execute isValid() for connection, configure connection test query. (null)
[info] application - Creating Pool for datasource 'default'
[error] c.z.h.p.PoolBase - HikariPool-3 - Failed to execute isValid() for connection, configure connection test query. (null)
[info] application - Creating Pool for datasource 'default'
[error] c.z.h.p.PoolBase - HikariPool-4 - Failed to execute isValid() for connection, configure connection test query.
...

Continue reading →


Implementing something like Django’s exception middleware in Play Framework

One thing that I’ve found to reduce boilerplate is being able to throw exceptions from helper methods and have those be converted to responses. For instance, I often want to see if an object exists, and if not, return a 404.

This is easy (and obvious) in Django - I can just create a middleware class and override the exception handling method. In Play it’s also easy, but it took a while to figure out where to put the logic. I think that I was looking for something as comprehensive as Django’s middleware - something that handles requests, responses, and exceptions, but that doesn’t exist in Play - those are all scattered around. To do something equivalent, you’d have to create a mixture of filters, actions, and creating your own ErrorHandler.

Looking at the exception handling alone, I find Django’s system to be much nicer out of the box. In Django, there is a list of middleware classes...

Continue reading →


Testing with Unmanaged Databases in Django

I’m testing some API endpoints in a Django app using two different databases. One of them is a read-only legacy SQL Server database (another topic in itself) with multiple schemas, while another is Postgres and stores things like authentication and sessions. Django isn’t great with SQL Server to begin with, but having it be unmanaged made things slightly trickier. Here is how I was able to get things working.

OK, this is a sample class:


class Foo(models.Model):
    id = models.PositiveIntegerField(
        primary_key=True, 
        db_column='FooId')
    bar = models.CharField(
        max_length=256, 
        db_column='BarEntr')

    class Meta:
        managed = False
        db_table = 'UNREADABLEENTERPRISENAME'

This is a pretty standard Django model. The differences are

  • You have to define the database columns that the model attributes are mapping to (the db_column argument)
  • ...

Continue reading →