Migration from Oracle to MySQL running on Amazon RDS

The final insult…

Oracle's demise

Apple should really consider their heightist policy too and sell a proper iMac stand!

Author:     Friday, January 28th, 2011 Code No Comments

Hibernate foreign-key collections and database best practice

I came across the following situation having noticed we were not enforcing database NOT NULL column constraints, even though all column entries were non-null. This is not best-practice from a database perspective and so I thought I’d look into it.

So, I have 2 tables, ENQUIRY and ELEMENT with a One-to-Many relationship such that an Enquiry has many Elements. And I wanted to enforce the NOT NULL constraint on foreign key column ELEMENT.ENQUIRY_ID. This relationship looks like so when modeling the Enquiry object with Hibernate:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "ENQUIRY_ID", referencedColumnName = "ID")
private Set elements = new HashSet();

When I enforced the NOT NULL constraint at the database level I received the following stacktrace however:

Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("ELEMENT"."ENQUIRY_ID")

So Hibernate is obviously persisting the collection of elements before the parent enquiry and then going back and doing an UPDATE on the foreign key field afterwards (so INSERT collection items with NULL foreign key, INSERT parent, UPDATE collection item foreign keys) as it doesn’t realise the foreign key column will always be non-null.

You must explicitly tell Hibernate that the foreign key column is NOT NULL. It can then persist the parent enquiry first followed by the collection items. To do this, add the following clause to the @JoinColumn annotation:

@JoinColumn(name = "ENQUIRY_ID", referencedColumnName = "ID", nullable = false)

This way, not only are you ensuring database best-practice, but Hibernate will also have to issue less statements (so INSERT parent, INSERT collection items) which should be a more efficient transaction statement.

The javax.persistence.JoinColumn API only states that the nullable clause indicates whether the foreign key column is nullable but nowhere could I find how this nuance affects Hibernate and the queries it issues – it’s probably out there someplace but I hope this post can help anyone with a similar problem.

Thanks to axtavt on stackoverflow for pointing out the obvious!

Author:     Sunday, January 9th, 2011 Database, Hibernate, Oracle No Comments

Mocking static method dependencies

Good article on refactoring “static cling“, have used this several times now:

@Service("securityContextRepository")
public final class SecurityContextRepositoryImpl implements SecurityContextRepository {
    @Override public SecurityContext getSecurityContext() {
        // Spring framework dependency that I rely on but do not wish to test directly
        return SecurityContextHolder.getContext();
    }
}

I can now mock the SecurityContextRepository implementation that is injected into my test cases and remove the reliance on a static method call.

Author:     Tuesday, October 19th, 2010 Code No Comments

Git revert a mistaken commit

You have 2 commits staged, and you want to keep the latest but revert the first. Easy when you know how:

git revert HEAD^

Git book – Fixing committed mistakes

Author:     Wednesday, October 13th, 2010 Code, Git No Comments

RESTful system design

A useful resource (see what I did there?) showing starting point and evolving maturity when designing RESTful systems, courtesy of the ubiquitous Martin Fowler.

Author:     Tuesday, July 13th, 2010 Architecture, Development, Internet, REST No Comments

Oracle 11g UCP with Tomcat

Download Instant Client Package – Basic and SQL*Plus from Oracle.
Unzip packages to /Applications/oracle/instantclient_10_2.
Add the following to ~/.bash_profile:

export DYLD_LIBRARY_PATH=/Applications/oracle/instantclient_10_2
export TNS_ADMIN=/Applications/oracle/instantclient_10_2
export PATH=$PATH:$DYLD_LIBRARY_PATH

Ensure you have the below tnsnames.ora file in TNS_ADMIN so as to connect to the server from client using connect string: sqlplus <username>@xyz


xyz =
(DESCRIPTION =
(ADDRESS =
(PROTOCOL = TCP)
(PORT = <port>)
(HOST = <server_name>)
)
(CONNECT_DATA = (SID = <SID>))
)
Add the following to your tomcat/conf/server.xml file:

<GlobalNamingResources>

<Resource
name="jdbc/<pool_jndi_name>"
auth="Container"
url="jdbc:oracle:thin:@<server_name>:<port>:<SID>"
user="<username>"
password="<password>"
factory="oracle.ucp.jdbc.PoolDataSourceImpl"
type="oracle.ucp.jdbc.PoolDataSource"
connectionFactoryClassName="oracle.jdbc.pool.OracleDataSource"
connectionPoolName="<pool_name>"
connectionWaitTimeout="30"
minPoolSize="5"
maxPoolSize="25"
inactiveConnectionTimeout="20"
timeoutCheckInterval="60"
validateConnectionOnBorrow="true"
sqlForValidateConnection="SELECT 1 FROM DUAL" />

...
and add the ucp.jar to tomcat/libs directory.

Author:     Wednesday, July 7th, 2010 Code, Database, Development, Oracle, Tomcat No Comments

Dozer examples

Best place to find Dozer example mapping files is in the source bundle under test/resources. Most other examples out there are pretty basic and won’t help you if you want to do something more complex…

Author:     Monday, July 5th, 2010 Code No Comments

Google Apps observations

After experimenting and persevering with Google Apps in a corporate environment I would say the following are the headline issues we have experienced:

Positives:

  • Fantastic cost/effort reduction over homegrown or outsourced alternatives
  • Simple setup and good online documentation and usability

Negatives:

  • Lack of full product features in Google Docs versus MS Office
  • Security features not entirely sufficient for a corporate environment

In general, for the cost, speed and effort with which you can set up a functioning environment I would say it’s definitely worth at least making sure you positively rule out Google Apps as an option for your corporate IT infrastructure.

The major issues we have found that might count against Google would be the inability to restrict usage to a certain IP range (i.e. the office) and poor Docs interoperability with MS Office. Until these are resolved corporate users will be slow on the uptake.

I think it’s a reasonable assumption that the products will improve rapidly – but I have found a lot of people with similar observations and no indication from Google that the security issues at least are being tackled!

Author:     Tuesday, March 16th, 2010 Code No Comments

Software Architecture Document

Really helpful Software Architecture Document guidelines. Worth a shout to codingthearchitecture.com, I like their “style” very much…

Author:     Sunday, February 21st, 2010 Architecture, Development No Comments

Agile Management

I have recently been involved with a project that had a great development team – project manager, BA, server-side and UI developers and a QA team – all co-located and pulling merrily and effectively along together towards the same objectives.

Whilst we undertook some pretty serious refactoring efforts as the Product Owner (rightly) demanded a change of direction and focus, there were numerous issues that arose as senior managers who had little knowledge or understanding of the project – or possibly more importantly Agile – came under increasing pressure to “do something” as the timelines wore on due to the change of direction.

Kelly Waters’ post on why Agile must be adopted and understood from the top-down pretty succinctly sums up many of the challenges we faced, particularly around stakeholder management and expectations.

As I’ve mentioned before in response to a similar post by Mark Schumann, it’s a shame and a surprise to me that those who do not understand or are not involved directly in an Agile project, but are nonetheless in positions of influence and supposed control, cannot do a little more to understand what is in essence a fairly simple, and I doubt particularly uncommon, scenario.

Author:     Monday, February 8th, 2010 Code No Comments

Agile CTO