I'm simply unable to inject EntityManagerFactory object from ProfileDBUtil into ProfileManager class where I actually apply the transactions to look for an entry in the database, or update a row and so.
createEntityManager() method throws the exception specified in the end of the entry.
The weird thing is that the same piece of code works for UAT environment while it fails for SIT environment. All config is the same for both of the environments. Class loader order, shared lib references, module class loaders are the same.
Both of the environments have the necessary jars in their file system and seem to be loaded successfully in runtime.
What would be the root cause for this problem?
public class ProfileDBUtil {
private static final String PERSISTENCE_UNIT = "com.profile.userdb";
public boolean loadProfile(String memberID) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
ProfileManager upm = new ProfileManager(emf);
List < Profile > ProfileList = upm.searchProfile(memberID);
}
}
#SuppressWarnings("unchecked")
#JPAManager(targetEntity = com.profile.userdb.model.Profile.class)
public class ProfileManager {
private EntityManagerFactory emf;
public ProfileManager() {
}
public ProfileManager(EntityManagerFactory emf) {
this.emf = emf;
}
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManager getEntityManager() {
if (emf == null) {
throw new RuntimeException("The EntityManagerFactory is null. This must be passed in to the constructor``");
}
return emf.createEntityManager(); // THIS FAILS
}
}
Log trace;
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115)
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1207)
at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:176)
at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89)
at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:125)
Edit of Neil; "JtaStatusHelper is a HIBERNATE class!" rang the bell for me. I went and checked the props of emf as the following;
Map<String, Object> prop = emf.getProperties();
logger.debug("emf props : " + prop.toString());
And observed the differences between SIT and UAT environments.
The basic difference was that openjpa was not available in any keys of the prop for SIT while it was all over the places for UAT. The config for SIT was somehow turned into hibernate related lines. However, I was out after having things in place according to JPA settings.
It simply proved that something is wrong with the last installation of the application into Server. Thus, I uninstalled the app and install it from scratch.
It made things right.
Related
I would like to have a properties setup which can, on certain environments, override specific properties. For example, our default JDBC properties for dev are:
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/ourdb
db.username=root
db.password=
The problem is that some of our devs would like to have a different username/password on the db, or possibly even a non locally hosted db. The same is true for our rabbitMQ configuration, which currently uses a similar localhost, guest/guest setup. Being able to override the properties of certain elements of this configuration on a per-developer basis would allow us to move much of the infrastructure/installation requirements for building the software off the local machine and onto dedicated servers.
I have set-up a simple project to wrap my head around the configuration required to achieve what I want, and this is my first foray into the world of spring property configuration, since up till now, property loading and management is done with some custom code. Here is my setup:
class Main_PropertyTest {
public static void main(String[] args) {
String environment = System.getenv("APPLICATION_ENVIRONMENT"); // Environment, for example: "dev"
String subEnvironment = System.getenv("APPLICATION_SUB_ENVIRONMENT"); // Developer name, for example: "joe.bloggs"
System.setProperty("spring.profiles.active", environment);
System.setProperty("spring.profiles.sub", subEnvironment);
try(AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PropertyTestConfiguration.class)) {
Main_PropertyTest main = context.getBean(Main_PropertyTest.class);
main.printProperty();
}
}
private final String property;
public Main_PropertyTest(String property) {
this.property = property;
}
public void printProperty() {
System.out.println("And the property is: '" + property + "'.");
}
}
And my configuration:
#Configuration
public class PropertyTestConfiguration {
#Bean
public static PropertySourcesPlaceholderConfigurer primaryPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource(System.getProperty("spring.profiles.active") + ".main.properties"));
return propertySourcesPlaceholderConfigurer;
}
#Bean
public static PropertySourcesPlaceholderConfigurer secondaryPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource(System.getProperty("spring.profiles.sub") + ".main.properties"));
propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
propertySourcesPlaceholderConfigurer.setOrder(-1);
return propertySourcesPlaceholderConfigurer;
}
#Bean
public Main_PropertyTest main_PropertyTest(#Value("${main.property}") String property) {
Main_PropertyTest main_PropertyTest = new Main_PropertyTest(property);
return main_PropertyTest;
}
}
And for completeness, my dev.main.properties and test.main.properties:
main.property=dev
main.property=test
The main problem is that I get an illegal argument exception. As far as I can tell, what I have written should be the javaconfig equivalent of this method: http://taidevcouk.wordpress.com/2013/07/04/overriding-a-packaged-spring-application-properties-file-via-an-external-file/
Unfortunately I get the following error: java.lang.IllegalArgumentException: Could not resolve placeholder 'main.property' in string value "${main.property}". Note that I also need to take care of the case where there is no sub-environment, and this is the case I have started with (although I get the same error even if both files exist). If I remove the bean which sets up the second propertysourcesplaceholderconfigurer, then it all works fine (by which I mean dev.main.properties is loaded and "And the property is: 'dev'." is printed out).
A secondary problem is that the code doesn't look great, and each layer of the system will need two PSPC's set-up so that they can access these properties. Furthermore, it requires a lot of manual calls to System.getProperty(), since I couldn't pass ${spring.profiles.active} to PSPC.setLocation();
Note: I have tried #PropertySources({primaryproperties, secondaryProperties}), but this fails because secondaryProperties does not exist. I have also tried #Autowired Environment environment; and getting the properties from that, but the secondary PSPC causes the environment to not be autowired...
So following this lengthy explanation, my questions are:
Is this the right way of solving this problem?
If so, what is wrong with my configuration?
How can I simplify the configuration (if at all)?
Is there an alternative mechanism available which would solve my problem?
Thank you for your time! :)
Your configuration is flawed when configuring BeanFactoryPostProcessor with java config the methods should be static. However it can be even easier, instead of registering your own PropertySourcesPlaceholderConfigurer utilize the default #PropertySource support.
Rewerite your jav config to the following
#Configuration
#PropertySource(name="main", value= "${spring.profiles.active}.main.properties")
public class PropertyTestConfiguration {
#Autowired
private Environment env;
#PostConstruct
public void initialize() {
String resource = env.getProperty("spring.profiles.sub") +".main.properties";
Resource props = new ClassPathResource(resource);
if (env instanceof ConfigurableEnvironment && props.exists()) {
MutablePropertySources sources = ((ConfigurableEnvironment) env).getPropertySources();
sources.addBefore("main", new ResourcePropertySource(props));
}
}
#Bean
public Main_PropertyTest main_PropertyTest(#Value("${main.property}") String property) {
Main_PropertyTest main_PropertyTest = new Main_PropertyTest(property);
return main_PropertyTest;
}
}
This should first load the dev.main.properties and additionally the test.main.properties which will override the earlier loaded properties (when filled ofcourse).
I had a similar issue with overwriting already existing properties in integration tests
I came up with this solution:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = {
SomeProdConfig.class,
MyWebTest.TestConfig.class
})
#WebIntegrationTest
public class MyWebTest {
#Configuration
public static class TestConfig {
#Inject
private Environment env;
#PostConstruct
public void overwriteProperties() throws Exception {
final Map<String,Object> systemProperties = ((ConfigurableEnvironment) env)
.getSystemProperties();
systemProperties.put("some.prop", "test.value");
}
}
I'm currently having the issue that the #Transactional annotation doesn't seem to start a transaction for Neo4j, yet (it doesn't work with any of my #Transactional annotated methods, not just with the following example).
Example:
I have this method (UserService.createUser), which creates a user node in the Neo4j graph first and then creates the user (with additional information) in a MongoDB. (MongoDB doesn't support transactions, thus create the user-node first, then insert the entity into MongoDB and commit the Neo4j-transaction afterwards).
The method is annotated with #Transactional yet a org.neo4j.graphdb.NotInTransactionException is thrown when it comes to creating the user in Neo4j.
Here is about my configuration and coding, respectively:
Code based SDN-Neo4j configuration:
#Configuration
#EnableTransactionManagement // mode = proxy
#EnableNeo4jRepositories(basePackages = "graph.repository")
public class Neo4jConfig extends Neo4jConfiguration {
private static final String DB_PATH = "path_to.db";
private static final String CONFIG_PATH = "path_to.properties";
#Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(DB_PATH)
.loadPropertiesFromFile(CONFIG_PATH).newGraphDatabase();
}
}
Service for creating the user in Neo4j and the MongoDB:
#Service
public class UserService {
#Inject
private UserMdbRepository mdbUserRepository; // MongoRepository
#Inject
private Neo4jTemplate neo4jTemplate;
#Transactional
public User createUser(User user) {
// Create the graph-node first, because if this fails the user
// shall not be created in the MongoDB
this.neo4jTemplate.save(user); // NotInTransactionException is thrown here
// Then create the MongoDB-user. This can't be rolled back, but
// if this fails, the Neo4j-modification shall be rolled back too
return this.mdbUserRepository.save(user);
}
...
}
Side-notes:
I'm using spring version 3.2.3.RELEASE and spring-data-neo4j version 2.3.0.M1
UserService and Neo4jConfig are in separate Maven artifacts
Starting the server and SDN reading operations work so far, I'm just having troubles with writing operations
I'm currently migrating our project from the tinkerpop-framework to SDN-Neo4j. This user creation-process has worked before (with tinkerpop), I just have to make it work again with SDN-Neo4j.
I'm running the application in Jetty
Does anyone have any clue why this is not working (yet)?
I hope, this information is sufficient. If anything is missing, please let me know and I'll add it.
Edit:
I forgot to mention that manual transaction-handling works, but of course I'd like to implement it the way "as it's meant to be".
public User createUser(User user) throws ServiceException {
Transaction tx = this.graphDatabaseService.beginTx();
try {
this.neo4jTemplate.save(user);
User persistantUser = this.mdbUserRepository.save(user);
tx.success();
return persistantUser;
} catch (Exception e) {
tx.failure();
throw new ServiceException(e);
} finally {
tx.finish();
}
}
Thanks to m-deinum I finally found the issue. The problem was that I scanned for those components / services in a different spring-configuration-file, than where I configured SDN-Neo4j. I moved the component-scan for those packages which might require transactions to my Neo4jConfig and now it works
#Configuration
#EnableTransactionManagement // mode = proxy
#EnableNeo4jRepositories(basePackages = "graph.repository")
#ComponentScan({
"graph.component",
"graph.service",
"core.service"
})
public class Neo4jConfig extends Neo4jConfiguration {
private static final String DB_PATH = "path_to.db";
private static final String CONFIG_PATH = "path_to.properties";
#Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(DB_PATH)
.loadPropertiesFromFile(CONFIG_PATH).newGraphDatabase();
}
}
I still will have to separate those components / services which require transactions from those which don't, though. However, this works for now.
I assume that the issue was that the other spring-configuration-file (which included the component-scan) was loaded before Neo4jConfig, since neo4j:repositories has to be put before context:component-scan. (See Note in Example 20.26. Composing repositories http://static.springsource.org/spring-data/data-neo4j/docs/current/reference/html/programming-model.html#d0e2948)
I am performing integration tests by using embedded Glassfish 3.1.2. The first thing I do in the test is to reset the database so each test have a completely fresh database to play with.
However, the problem is that the objects are persisted in the shared cache and not stored in the database. So when the next test starts it will get the old records from the cache instead of the database.
I can easily get rid of the problem by define
<property name="eclipselink.cache.shared.default" value="false"/>
in my persistence.xml file.
#BeforeClass
public static void startup() throws Exception {
container = EJBContainer.createEJBContainer();
context = container.getContext();
}
#Before
public void setUp() throws Exception {
//Clean database before every test using dbunit
}
#Test // This is the first test, works well since the test is first in order
public final void testCreateUser() throws Exception {
UserService userService = (UserService) context.lookup("java:global/galleria/galleria-ejb/UserService");
User user = new User(TEST_USER_ID, TEST_PASSWORD);
User actualUser = userService.signupUser(user);
assertTrue(actualUser != null);
assertEquals(TEST_USER_ID, actualUser.getUserId());
assertFalse(Arrays.equals(TEST_PASSWORD, actualUser.getPassword()));
logger.info("Finished executing test method {}", testMethod.getMethodName());
}
#Test // This is the second test, fails since the database not is clean
public final void testCreateUser() throws Exception {
UserService userService = (UserService) context.lookup("java:global/galleria/galleria-ejb/UserService");
User user = new User(TEST_USER_ID, TEST_PASSWORD);
User actualUser = userService.signupUser(user); // FAILS since TEST_USER_ID already in cache!!
//..
}
#Stateless
#EJB(name = "java:global/galleria/galleria-ejb/UserService", beanInterface = UserService.class)
#TransactionAttribute(TransactionAttributeType.REQUIRED)
public class UserServiceImpl implements UserService
{
#EJB
private UserRepository userRepository;
#Override
#PermitAll
public User signupUser(User user) throws UserException {
User existingUser = userRepository.findById(user.getUserId());
if (existingUser != null)
{
logger.error("Attempted to create a duplicate user.");
throw new UserException(DUPLICATE_USER);
}
try {
user = userRepository.create(user);
} catch (EntityExistsException entityExistsEx) {
logger.error("Attempted to create a duplicate user.");
throw new UserException(DUPLICATE_USER, entityExistsEx);
}
return user;
}
//..
}
However, I do not want to disable caching in persistence.xml file, since I will get performance loss later on. I only want to do it while testing. Note that I am using JTA data source here.
Any ideas?
Off topic, I am trying to learn java ee, and following the Galleria EE project and try to modify it for my needs.
Best regards
Check out http://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching
as both JPA 2.0 and EclipseLink native api allow clearing the shared cache. You could call this api at the start or end of your tests.
I'm developing an application in Spring 3.0.5 and Hibernate 3.6.2, and currently i'm working in a JSON controller, but i have this exception and i can't understand why is happening. I've checked previously in SO and Google, but this problem is quite strange. So here is my code:
Controller
#RequestMapping(value = "/props", method = RequestMethod.GET)
public #ResponseBody
List<Property> getJsonProps(String id) {
if(id==null)return null;
Device dev = deviceService.getDispositivo(Long.parseLong(id));
List<Property> props = deviceService.listProperties(dev, 10);
return props;
}
Device Service
#Service("manageDevices")
#Transactional(readOnly=true,propagation=Propagation.REQUIRED)
public class ManageDevicesImpl implements ManageDevices {
private Logger log = LoggerFactory.getLogger(getClass());
#Autowired
private DevicesDAO devicesDAO;
public List<Property> listProperties(Device dev, Integer qty) {
List<Property> props = devicesDAO.pickProperties(dev, qty);
return props;
}
}
DAO
#Repository("devicesDAO")
public class DevicesDAOImpl implements DevicesDAO {
private Logger log = LoggerFactory.getLogger(getClass());
#Autowired
private SessionFactory sessionFactory;
public List<Property> pickProperties(Device dev, Integer qty) {
if(qty >= 0){
log.debug("Open? "+ sessionFactory.getCurrentSession().isOpen());
log.debug("Tx Active? " + sessionFactory.getCurrentSession().getTransaction().isActive());
List<Property> props = dev.getProperties();
if(props != null){
if(props.size() >= qty)
return props.subList(0, qty-1);
else
return props;
}
}
return null;
}
}
The exception occurs in the pickProperties function (DAO Layer), at the line where i try to load the properties (getProperties). In the logs, there is an open session and transaction. Thanks in advance.
Could you post the exact exception you got?
You're in a transaction at the dev.getProperties() line, but not the transaction where dev was loaded. You either need to reattach it, or arrange for dev.getProperties() to be called while you're still in the transaction that loaded it, or move the transactional boundary up so that both calls are in the same transaction, or change the Hibernate configuration so that properties isn't lazy loaded, or change the code that loads dev so that it fetches properties in HQL.
Which of those options will apply to you depends on your situation, but I'd start with the last one.
Looks like your transaction is created after the Device dev is read. Try reading/rereading it within the transaction to see what happens.
Ok, I read bunch of articles/examples how to write Entity Manager Factory in singleton.
One of them easiest for me to understand a bit:
http://javanotepad.blogspot.com/2007/05/jpa-entitymanagerfactory-in-web.html
I learned that EntityManagerFactory (EMF) should only be created once preferably in application scope.
And also make sure to close the EMF once it's used (?)
So I wrote EMF helper class for business methods to use:
public class EmProvider {
private static final String DB_PU = "KogaAlphaPU";
public static final boolean DEBUG = true;
private static final EmProvider singleton = new EmProvider();
private EntityManagerFactory emf;
private EmProvider() {}
public static EmProvider getInstance() {
return singleton;
}
public EntityManagerFactory getEntityManagerFactory() {
if(emf == null) {
emf = Persistence.createEntityManagerFactory(DB_PU);
}
if(DEBUG) {
System.out.println("factory created on: " + new Date());
}
return emf;
}
public void closeEmf() {
if(emf.isOpen() || emf != null) {
emf.close();
}
emf = null;
if(DEBUG) {
System.out.println("EMF closed at: " + new Date());
}
}
}//end class
And my method using EmProvider:
public String foo() {
EntityManager em = null;
List<Object[]> out = null;
try {
em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
Query query = em.createNativeQuery(JPQL_JOIN); //just some random query
out = query.getResultList();
}
catch(Exception e) {
//handle error....
}
finally {
if(em != null) {
em.close(); //make sure to close EntityManager
}
//should I not close the EMF itself here?????
EmProvider.getInstance().closeEmf();
}
I made sure to close EntityManager (em) within method level as suggested. But when should EntityManagerFactory be closed then? And why EMF has to be singleton so bad??? I read about concurrency issues but as I am not experienced multi-thread-grammer, I can't really be clear on this idea.
EntityManagerFactory instances are
heavyweight objects. Each factory
might maintain a metadata cache,
object state cache, EntityManager
pool, connection pool, and more. If
your application no longer needs an
EntityManagerFactory, you should
close it to free these resources.
When an EntityManagerFactory closes,
all EntityManagers from that factory,
and by extension all entities managed
by those EntityManagers, become
invalid.
It is much better to keep a factory
open for a long period of time than
to repeatedly create and close new
factories. Thus, most applications
will never close the factory, or only
close it when the application is
exiting.
Only applications that require
multiple factories with different
configurations have an obvious reason
to create and close multiple
EntityManagerFactory instances.
Only one EntityManagerFactory is
permitted to be created for each
deployed persistence unit
configuration. Any number of
EntityManager instances may be
created from a given factory.
More than one entity manager factory
instance may be available
simultaneously in the JVM. Methods of the EntityManagerFactory
interface are threadsafe.