How to solve the “Double-Checked Locking is Broken” Declaration in Java?

Here is the idiom recommended in the Item 71: Use lazy initialization judiciously of Effective Java: If you need to use lazy initialization for performance on an instance field, use the double-check idiom. This idiom avoids the cost of locking when accessing the field after it has been initialized (Item 67). The idea behind the … Read more

Disable lazy loading by default in Entity Framework 4

The following answer refers to Database-First or Model-First workflow (the only two workflows that were available with Entity Framework (version <= 4.0) when the question was asked). If you are using Code-First workflow (which is available since EF version >= 4.1) proceed to ssmith’s answer to this question for a correct solution. The edmx file … Read more

JQuery to load Javascript file dynamically

Yes, use getScript instead of document.write – it will even allow for a callback once the file loads. You might want to check if TinyMCE is defined, though, before including it (for subsequent calls to ‘Add Comment’) so the code might look something like this: $(‘#add_comment’).click(function() { if(typeof TinyMCE == “undefined”) { $.getScript(‘tinymce.js’, function() { … Read more

Entity framework linq query Include() multiple children entities

Use extension methods. Replace NameOfContext with the name of your object context. public static class Extensions{ public static IQueryable<Company> CompleteCompanies(this NameOfContext context){ return context.Companies .Include(“Employee.Employee_Car”) .Include(“Employee.Employee_Country”) ; } public static Company CompanyById(this NameOfContext context, int companyID){ return context.Companies .Include(“Employee.Employee_Car”) .Include(“Employee.Employee_Country”) .FirstOrDefault(c => c.Id == companyID) ; } } Then your code becomes Company company = … Read more

How to fix org.hibernate.LazyInitializationException – could not initialize proxy – no Session

If you using Spring mark the class as @Transactional, then Spring will handle session management. @Transactional public class MyClass { … } By using @Transactional, many important aspects such as transaction propagation are handled automatically. In this case if another transactional method is called the method will have the option of joining the ongoing transaction … Read more