Primary Key generation MySQL Hibernate - mysql

I have an application deployed across 2 instances.
Database: MySQL
ORM: Hibernate
However, I need to implement an Oracle sequence like behaviour. Since MySQL doesn't have any, I simply created a table with an AUTO_INCREMENT and a method to return the value from it. It's thread-safe , so its not a problem when I deploy this application on 1 server. However, I dont think this thread-safe behaviour will hold true across multiple JVMs.
What to do in this case?

It is safe to use across multiple JVMs. MySQL issues each ID once. Use getGeneratedKeys() on a ResultSet to retrieve the generated ID.

Related

Knex : universal way to get the last inserted id

I'm using Knex, because I'm working on an application that I would like to use with multiple database servers, currently Sqlite3, Postgres and MySQL.
I'm realizing that this might be more difficult that I expected.
On MySQL, it appears that this syntax will return an array with an id:
knex('table').insert({ field: 'value'}, 'id');
On postgres I need something like this:
knex('table').insert({ field: 'value'}, 'id').returning(['id']);
In each case, the structure they return is different. The latter doesn't break MySQL, but on SQlite it will throw a fatal error.
The concept of 'insert a record, get an id' seems to exist everywhere though. What am I missing in Knex that lets me write this once and use everywhere?
Way back in 2007, I implemented the database access class for a PHP framework. It was to support MySQL, PostgreSQL, SQLite, Microsoft SQL Server, Oracle, and IBM DB2.
When it came time to support auto-incremented columns, I discovered that all of these implement that feature differently. Some have SERIAL, some have AUTO-INCREMENT (or AUTOINCREMENT), some have SEQUENCE, some have GENERATED, some support multiple solutions.
The solution was to not try to write one implementation that worked with all of them. I wrote classes using the Adapter Pattern, one for each brand of SQL database, so I could implement each adapter class tailored to the features supported by the respective database. The adapter satisfied an interface that I defined in my framework, to allow the primary key column to be defined and the last inserted id to be fetched in a consistent manner. But the internal implementation varied.
This was the only sane way to develop that code, in my opinion. When it comes to variations of SQL implementations, it's a fallacy that one can develop "portable" code that works on multiple brands.

Using GUID for MySQL membership provider user

I'm using the MySql membership provider with a .NET MVC 4 application and got it all set up as per this tutorial.
The problem is that the provider sets up the mysql_aspnet_users table with the UserID PK as an INT(11), whereas the MSSQL provider uses a UNIQUEIDENTIFIER.
I need to migrate existing users to this database and I would much prefer to keep a Guid as the primary key within the Users and Membership tables.
Is there any way to do this with the MySql membership provider?
Or do I need to write my own MySql membership provider just to use UUIDs as the primary keys?
I've had a look for any documentation or "non-hacky" ways to do this but haven't had any luck so far.
I dont think you can prevent creating a custom membership user class with a custom membership provider. Something like this tutorial http://msdn.microsoft.com/en-us/library/ms366730%28v=VS.85%29.aspx they use GUID's as wel. You need to change the SQL abit so it works with MySQL
You can store a guid as a CHAR(16) binary if you want to make the most optimal use of storage space.
or varchar(36) if its ok .
http://mysqlbackupnet.codeplex.com/wikipage?title=Using%20MySQL%20With%20GUID%20or%20UUID
At work we ended up creating our own MySql membership provider for .NET and it's called Dolphin https://github.com/film-skills/dolphin-net! It also includes a role provider, and allows you to specify in the config whether you use a GUID or an Integer. It's still in very early days and hasn't been tested thouroughly in production though it's a start! Also on NuGet http://www.nuget.org/packages/Dolphin/0.0.2

MySQL Workbench - Fix Index Names Warning

I have a new client that is running a Symfony application with 170 or so MySQL tables. He recently updated his MySQL Workbench to the latest revision and is now getting a warning pop-up when he launched the application
FIX INDEX NAMES
Index names identical to FK names were found in the model, which is not allowed for MySQL5.5 and later. Would you like to rename the indexes?
I am not a DBA but I understand that the index and primary key names are clashing. What are the implications of renaming these indexes vs. just ignoring?
The Symfony app he is running uses the Doctrine ORM, would any queries or the model need to be updated should the indexes be renamed?
Using the InnoDB engine
Thanks
MySQL Workbench checks at opening a model if there are any duplicate index names and offers to rename them to be unique. Letting it doing this has not bad side effect. In fact it is even necessary to be able to apply the model to a server. Otherwise the server will refuse to create tables that contain an index with a name that was already taken.
So in short: it's a good idea to let Workbench fix this bug (since duplicate key names are nothing but a bug).

MySql Entity Framework Database First - Primary Key = Long?

I am attempting to use the MySql Entity Framework provider for a small project. I've always encountered "quirks" when working with the MySql Connector (the one developed by MySQL, not DevArt).
The latest thing I found was that my entities generated from the database are getting long instead of int for their Id fields even though the type in the database is specified as a SIGNED INTEGER, AUTO INCREMENT, PRIMARY KEY.
The only thing I can think of is that LAST_INSERT_ID() always returns long. Since my entities rely on the database to create the identity value, perhaps MySQL Connector makes the Ids long to accommodate the id retrieval after insert?
The easiest workaround for all the issues I've encountered would be to just use SQL Server. However, the target for this app is shared hosting. Even if you pay for a SQL Server add-on, most shared hosts limit the SQL Server instances to 200MB and only allow 1 or 2 databases. That is just too small in my opinion. The same hosts offer several 1GB MySQL databases for free. If you outgrow that, you probably should have a dedicated box anyway.
MySQL will allways return a 64 bit int (or a BIGINT) instead of a 32 bit int. That's why you're getting a long instead of an int.
See this bugreport: http://bugs.mysql.com/bug.php?id=64084, and the MySQL manual on LAST_INSERT_ID(): http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id.

Bltoolkit - Dealing with identity with multiple db's

How do I deal with identity when I'm supporting multiple db's with Bltoolkit. I know that BL supports InsertWithIdentity call with linq whne doing inserts, but I think it only works with Sql Server and in this instance I don't want to use it in this instance
Is their a better way doing. Pehaps creating some kind of identity map to store the last primary key value for a particular entity stored.
Any advice would be helpful.
Thanks
scope_creep
MySql supports AUTO_INCREMENT, so it's not a problem.
For Oracle there are two ways to implement identity:
Define a trigger.
Use a sequence.