Friday 27 February 2015

"The transaction associated with the current connection has completed but has not been disposed." Well, not really

I was trying to solve another problem, that operations creating, altering or dropping databases cannot be made inside a .Net transaction. Therefore I created a new TransactionScope inside the main one, using the TransactionScopeOption.Suppress option, but then I started getting this weird exception: The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements. But I did Complete and Dispose all my transaction scopes, so what was going on? Long story short, this is really confusing message for a simple problem: your transaction probably timed out. Create the transaction scope with a large TimeSpan as the Timeout and it will get through. If you can, you can use a try/catch/finally block in which you Dispose the transaction (just remember that a using construct is basically a try/finally block). In my case, I was conditionally creating this new TransactionScope, so I wasn't using using. The transaction would fail, bleed into the other transaction where the confusing exception was being thrown.

Thursday 26 February 2015

T-SQL AVG function fails when SUM fails

I have another SQL quirk for you today, particularly in Microsoft's SQL Server. It concerns the AVG function when the SUM of the averaged values would result in an overflow for that type: AVG fails with the same error. Let's imagine for a moment that we wouldn't have an AVG function. In that case, our code would use SUM and COUNT to average values. For the integer column x, the naive SUM(x)/COUNT() would fail when the sum goes over the INT maximum value (I am not sure that SUM should fail, either, but that is another discussion). So our solution would be something like CONVERT(INT,SUM(CONVERT(BIGINT,x)))/COUNT(). Obviously, this is the solution for the AVG issue, just average the values converted to the biggest type available. So it seems that in their flagship SQL server, Microsoft implemented the naive version, hmm... To be fair, this behaviour is documented in the T-SQL function page: If the sum exceeds the maximum value for the data type of the return value an error will be returned.

As I see it, it is a bug, as this should have been handled internally, and one is logged in Microsoft Connect here: AVG function causes Arithmetic overflow error. Read the funny comment from some Microsoft employee who says there is a solution proposed, but they don't have time to implement it in SQL Server 2008, so probably they will do that in a future version. I'm trying on SQL Server 2014; it's 7 years later, Microsoft!!

Just for kicks, I tried the same in MySQL and it works there. More than that, it doesn't fail for SUM, either, as it automatically returns a BIGINT value, pushing the issue to an eventual insert or update in an INT column.

Thursday 19 February 2015

Star Trek New Voyages - Simply Amazing!

A while ago I wrote a post about "unofficial" Star Trek series and movies, made by fans for the fans. Some quite awful, to my chagrin, but some quite engaging and with good production values. But I have to say that Star Trek New Voyages is just wonderful. Yet another series continuing the original, with captain Kirk at the helm of the Enterprise, it has some of the best Star Trek scripts I have ever seen and the acting is not bad, considering they are all amateurs, and the production values are good, considering that it is made by German studios (in English with American actors, have no fear!)

When I fell in love with Star Trek I did it in the time of Jean Luc Picard and The Next Generation. As much as it irks me to admit it, I liked it for the same reason my father liked it: the stories! The sci-fi was great, but it only enhanced what was already there: great stories about real people in real situations, focusing on positive traits like friendship, loyalty, love, intelligence, skill, courage, happiness, passion. With Star Trek, people have found that there is a higher ideal that they can aspire to.

Well, New Voyages has all of that: dedicated people doing the series out of passion, having the courage to get together and do something out of friendship and loyalty, but with intelligence, skill and soul. I present to you a team of people actually living their dream, and making the fans dreams come true as well. Just great stuff! And if it isn't enough, all the episodes are free to watch on their website, in high definition and with subtitles in several languages. And just in case you were wondering, original actors from the original series like George Takei (Sulu) and Walter Koenig (Chekov) are helping out.

Just watch it, it's just fantastic! I leave you with one of the best episodes (so far): World Enough and Time

Wednesday 18 February 2015

House of Cards, by Robyn Dawes

Book cover House of Cards - Psychology and Psychotherapy Built on Myth is a very good book that needs more recognition. It describes and really criticizes the lack of scientific method in psychology and debunks the myth of the experienced psychologist as well as many others that are now taken for granted in the field. Unfortunately the book is also very detailed, filled with expositions, repetitions of concepts and statistical information on the studies that prove the author's point, so it is rather difficult to read; it is certainly not a book you take to help you relax. Every psychologist in the world should read it, though, as well as any aspiring students or people considering going to therapy.

To make it clear, this is not an anti-psychology book. It continuously says that therapy helps. What it also says is that the amount of training and experience of the therapist is statistically irrelevant. That irritates the author tremendously, as he is a psychologist himself and desires that his chosen scientific field evolve and ... well... become a real science. Robyn Dawes unfortunately died in 2010, at the ripe age of 74. During his life he studied human irrationality, intuitive expertise and statistical applications in medicine and psychology. No wonder that in House of Cards, he is ranting against the practice of psychology as it is today.

A few concepts in the book are very interesting and quite frightening. After WW2, a lot of people came traumatized and needed mental attention. At the time, a psychologist needed to be a psychiatrist as well, having gone through the university and studied medicine; they were all doctors, with a Ph.D. degree. So what they did in the US was to create another type of degree, called Psy.D, letting people without medical training enter the field, with only minimal instruction. This created the myth of the intuitive expert who can tell things about people because he has experience, having little else. Dawes proceeds to mercilessly debunk this myth.

In order to do that, he uses - what else - the scientific method. He gathers data as objectively as possible and then tries to find correlations. One correlation that is not found is one between amount of experience (or indeed, formal training) and positive results. One that is, though, is that therapy does help. We just don't know (or better said, we don't know how to quantify) why. One obvious reason would be that, in order to come to therapy, people need to accept they have a problem and then make the first step in solving it: showing up. This alone shows that the person is already actively pursuing healing, a major step into healing themselves. He also analyses diagnosis, often using standardized tests that presumably can help a specialist determine mental issues and their type. However, presented only with test results, the experts don't really get to any useful conclusion.

Dawes is not stopping at psychology, even if that is his main focus. In one chapter he speaks of studies that have proven that doing a thing for a long time doesn't necessarily teach you anything, especially if there is no immediate feedback on whether what you did was good or bad. This also applies to some types of medical diagnosis. And yes, those people went through school - that has the main purpose to promote people who can get through it much more than to provide a comprehensive body of knowledge - and graduated, but when faced with ambiguous symptoms, they pretty much randomly guess what the patient is afflicted by. Think about that when you go to just one doctor and he tells you that he knows what you have because he's experienced.

Anyway, as I said, the book is difficult to read, it is more like a scientific paper and, as much as I wanted to finish it, I realize that I am not an aspiring psychologist, nor am I planning to go to therapy soon. Also, since I have people close to me interested in the field, it wouldn't help to talk to them about how they don't use the scientific method and they are not real doctors ;). Joke aside, this book is invaluable for anyone in the field. Not for me, though, and so I decided to indefinitely postpone reading it to the end.

Underworld Ascendant, Hero-U, Star Trek Continues and Axanar, the birth of Kickstarter entertainment

In this post I want to talk to you about new stuff that links to the good old stuff of our own youth. You probably know what Kickstarter is, but just as an introduction, it is a place where people ask for money for future work. It's like a crowdsourced financing scheme for your public elevator pitch (just imagine a planet-sized elevator, though). And when I say Kickstarter, I mean the actual site and all the other similar things out there. Like... Kickstarter-like, like it?

First stop: Underworld Ascendant. The team that made Ultima Underworld, one of my all time favourite games, is doing a new one. As you can see on the Kickstarter page, it is two weeks from completing. If you loved the Ultima Underworld games (NOT the Ultima games), you could consider pitching in.

Second stop: Hero-U. Remember Quest for Glory? It was made by Sierra Games and the entire series was awesome! However the designers of the game are the Coles. They have been working on Hero-U, a modern version of the QG universe. They planned to release in the spring of 2014, but scope creep and public feedback turned the game from a simple little game to a complex and interesting concept that is planned for release in the autumn of 2015 and it is well on schedule. Check it out! They are at their second Kickstarter round.

Turning to movies and series, this time works made by and for Star Trek fans. And I am not even talking about random people doing really weird and low quality stuff, I mean real movie business people doing great stuff. Check out Star Trek Continues, a continuation of the original Star Trek series, as well as Star Trek Axanar, which seems to become a really cool movie! I can't wait for it to get out.

Update June 27th 2016:
The Axanar story has become a poster for corporate greed and stupidity. Soon after the trailers for Axanar were released, Paramount and CBS - the corporations owning the Star Trek franchise - sued the producers on copyright infringement. Funny enough, they did this before anything real was released. Their problem? The production was too big.

Having received more than 1.2 million US dollars from Kickstarter, the show was actually starting to look great. Top production qualities, professional actors, good CGI and - most of all - passionate people. Paramount and CBS alleged that this was already a commercial venture, having such budget, even if it was released freely on the Internet after production. To me, it feels as if Hollywood started to feel the heat. They realized that if this production and distribution model catches on, they will be left trying to combat piracy and hiring armies of lawyers to arrange and check distribution contracts when "the opposition" will just release free on the Internet once the budget for production is met. Consider the implications! This would be huge.

It felt like entrapment. First you let legions of people use the Star Trek moniker and universe, then you jump with a lawsuit on the people that make the most money. So the studios started to try to deflect the anger and consternation of fans and independent producers with dirty tricks like instructing J.J.Abrams to say in an interview that the lawsuit would go away, only for it to continue anyway and finally, with a set of guidelines for independent productions to which the studios would not object. The terms are ridiculous and pretty much break the entire concept of serialized Star Trek. More here, check this out: “The fan production must … not exceed 30 minutes total, with no additional seasons, episodes, parts, sequels or remakes.”



A long time ago I wrote a post about Vodo, what I thought was the future of cool little indie movies and series. Vodo didn't quite live to my expectations, but Kickstarter has taken its place and, since it is not only about movies, but all kinds of projects, it has a larger chance of surviving and changing the way the world works. Not all is rosy, though. There are voices that say that the Kickstarter ecosystem is more about promises than about delivery. Also some governmental and commercial agencies are really displeased with the way money are exchanged directly between customers and producers, bypassing borders, intermediaries like banks and tax collectors and so on. If you combine this with Bitcoin type currency, their job of overseeing all commercial transactions and taking their cut does become more difficult. I sympathise... not really.

I leave you with some videos of the projects above. Think about looking for others that are working on something you want to sponsor. You might be surprised not only by the ingenious ideas that are out there, but also about how it would make you feel to support people with the same passions as yourself.

Underworld Ascendant trailer:


Game play for Hero-U:


The full first episode of Star Trek Continues from the creators themselves:


Prelude to Axanar, a small mockumentary about the events that will be the context of Axanar:

Monday 16 February 2015

NuGet insanity (Could not load file or assembly 'System.Web.WebPages, Version=3.0.0.0...)

I am relatively new to the entire NuGet ecosystem. What I expected is for things to just work. You know... Microsoft. However the web of interdepencies seems to be too much even for them. The problems that appear when updating MVC versions, .NET Framework versions, etc, are as annoying as they are unclear. One example: I was trying to publish a project that worked perfectly on my system. I moved it to the server machine, and weird things began to happen. The most annoying of them all is that the errors that occur do that at runtime instead of at compile time. Such an error was "Could not load file or assembly System.Web.WebPages, Version=3.0.0.0...". The project reference for WebPages was 2.0.0.0. If I removed it and tried to add a reference, only versions 1.0.0.0 and 2.0.0.0 were available. Meanwhile Razor was complaining that it didn't find the 3.0.0.0 version of WebPages.

Long story short: don't try to resolve only the library dependencies, but also the framework dependencies. In this case, System.Web.WebPages 3.0.0.0 is only available for the .NET framework 4.5.1. The project was configured as 4.5. Updating the MVC framework after the change to .NET 4.5.1 solved it.

Steps:
  • Change the project version to 4.5.1 (or whatever the newest usable .NET framework version)
  • go to the NuGet Package Manager Console in Visual Studio
  • Run command Update-Package -reinstall Microsoft.AspNet.Mvc

This, of course, is not a panacea for all problems, but just remember that the .NET framework is important in these scenarios.

Thursday 12 February 2015

Always send the CORS headers with your HTTP error responses

I met this situation where I wanted to implement an http interceptor, a piece of JavaScript code that would do something on unauthorized access to my API. The way to do this is irrelevant (and different from jQuery and AngularJS sites), but there is a problem that affects every situation and that is when you access the API from a different domain than the API's. You see, the browser needs the API server to authorize CORS for every Ajax request that accesses that server from another domain. You might think you did that already in your API, but let me ask you: when there is a problem, like not authorized access, are you sending CORS headers with your error response? Because if you do not, everything you send, including the http code, will not be parsed by the browser and any interception will just show a code of 0. The situation is a little confounded by the fact that the browser does announce that you have a CORS access problem, but also displays the status message, which in this case would be "Unauthorized access". This might make you think you can access that message or the status code. Well, you cannot.

The solution is to send the CORS headers: Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers. Just set their value to "*" and send them with your error response and now your JavaScript can read the error code and message.

Saturday 7 February 2015

A light speed trip from the Sun to Jupiter

This is a great film. It shows what it would look like to travel with the speed of light from the Sun to Jupiter. It takes 45 minutes (the length of the film) and it is one of the few things that show really how space is, how large, empty, unforgiving it is and how small and almost insignificant are the small islands of dirt we are fixated on. Don't worry, you have a little indicator of when something is about to appear, so you can fast forward. Really loved this. I only wish it would have continued to Pluto and beyond (just so you understand how awesome the New Horizons probe really is)

Thursday 5 February 2015

Javascript "arguments" weirdness

I was using this JavaScript function that I wanted to accept an array of arguments or just a bunch of arguments that would be interpreted as an array. As you probably know, for each function in JS you get a variable called arguments which contains the arguments to the function. It is a pseudo array, not a real array, and has some extra properties. My code looked like this:
function x(arg) {
if (!arg) {
arg=[];
} else if (!arg.length) {
arg=[];
for(var i=0; i<arguments.length; i++) arg.push(arguments[i]);
}
// do something with arg
}

The logic is simple, if there is no first parameter, arg becomes an empty array, else, if there is a first argument, but it doesn't have a length property (not an array) set arg to an array and push all arguments of the function as items of the array. But it doesn't work! The point is this: you set arg to an empty array and at that moment arguments[0] is no longer the original argument, but the empty array. Even worse, the code then adds the array as an item of itself, which makes the object be infinitely recursive.

Let's make this simpler:
function x(arg) {
arg=[];
console.log(arguments[0]);
}
After you execute x() with any arguments, the console will show an empty array, not the original argument. Weird, huh?

Monday 2 February 2015

Childhood's End, by Arthur C. Clarke

Book cover Childhood's End is a really interesting book. It is actually a series of medium length stories set in the same world. It starts with a sort of Independence Day kind of invasion - a peaceful one, though - it continues with the effects on the human world after decades have passed and then it ends with a human evolutionary leap that explains the entire book so far. The reason why I loved it so much is that a vast majority of the ideas in the book have withstood the test of time. Written in 1953, Childhood's End is remarkably modern and rational. Well, maybe today's world is not particularly rational, so maybe it is more modern that the present, which is remarkable :)

Arthur C. Clarke seems to have had an obsession with alien encounters - and by this I mean advanced species that have good reason to come to Earth, other than wanting to steal our water or mine our gold or other stupid thing like that. He wrote 2001: a Space Odyssey and Rendezvous with Rama, both about humans suffering of culture shock after a meeting with an alien species. Personally I think Rendezvous with Rama should not have had sequels, perhaps even Space Odyssey; for me it seems like Clarke continued some stories that had great success, rather than needing to continue those stories.

Anyway, Childhood's End is like that: alien creatures just oversee the evolution of our species on Earth, intervening only on minimal occasions. I loved the idea because it is a quick and dirty sci-fi solution for historical and all too present issues like borders, religion, corruption, politics and all those ugly things that appear like magic when enough people get together. I also loved the kind of Christian metaphor of daemons being directed to oversee and guide the human race, without them being privy to "God's grace", so to speak.

It is not an easy to finish book, as it isn't really an emotional story. There are no heroes that one can identify with; it is just a descriptive, rational, logical narration. It is a good book, though, one that I am glad to have read listened to as an audio book.