开发者

Could not open JPA EntityManager for transaction in Spring Test

开发者 https://www.devze.com 2023-04-12 22:08 出处:网络
I have a java project using Spring 3.0 , JPA 2.0 with MyEclipse IDE Trying to convert some basic dao integration tests to spring and have run into a few issues. Here\'s my setup:

I have a java project using Spring 3.0 , JPA 2.0 with MyEclipse IDE

Trying to convert some basic dao integration tests to spring and have run into a few issues. Here's my setup:

LevelDAO

public class LevelDAO extends JpaDaoSupport implements ILevelDAO 
{
public void save(Level entity) {
    logger.info("saving Level instance");
    try {
        getJpaTemplate().persist(entity);
        logger.info("save successful");
    } catch (RuntimeException re) {
        logger.error("save failed", re);
        throw re;
    }
}

}

Unit Test

@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
public class LevelDaoImplTests {

 LevelDAO levelDao;

 @Test
 public void shouldSaveNewLevels() {

        levelDao= new LevelDAO();
        Level l = new Level();   
        l.setName = "test";
        levelDao.save(l); 

    }

}

Persistence.Xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="t1" transaction-type="RESOURCE_LOCAL">
    <provider>
        org.eclipse.persistence.jpa.PersistenceProvider
    </provider>
    <class>com.nlg.model.Level</class开发者_JS百科>
    <properties>
        <property name="javax.persistence.jdbc.driver"
            value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url"
            value="jdbc:mysql://myserver.rds.amazonaws.com:3306/" />
        <property name="javax.persistence.jdbc.user" value="myuser" />
        <property name="javax.persistence.jdbc.password" value="mypass" />
    </properties>
</persistence-unit>

applicationContext.xml

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="t1" />
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean
    id="LevelDAO" class="com.nlg.model.LevelDAO">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>

At first I recieved this

Value '2.0' of attribute 'version' of element 'persistence' is not valid

So reading another post which suggested changing this to "1.0"

and now recieve

java.lang.IllegalStateException: Failed to load ApplicationContext

That issue was resolved with the < provider > tag however hasn't fixed my issue:

org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is Exception [EclipseLink-4021] (Eclipse Persistence Services - 1.0.2 (Build 20081024)): org.eclipse.persistence.exceptions.DatabaseException Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].

My concern is that by downgrading the version no. to "1.0" that I'm also overlooking the issue of compatibility within this stack. I have non-spring versions of these tests working, so any ideas on where I've gone wrong appreciated.

Thanks


  • Unable to acquire a connection from driver [null], user [null] and URL [null]

These are JPA 2.0 properties:

 <property name="javax.persistence.jdbc.driver"
        value="com.mysql.jdbc.Driver" />
 <property name="javax.persistence.jdbc.url"
        value="jdbc:mysql://myserver.rds.amazonaws.com:3306/" />
 <property name="javax.persistence.jdbc.user" value="myuser" />
 <property name="javax.persistence.jdbc.password" value="mypass" />

So if you switched back to 1.0, you'd probably want:

<property name="eclipelink.jdbc.url" value="jdbc:mysql://myserver.rds.amazonaws.com:3306/"/> 
<property name="eclipelink.jdbc.user" value="myuser"/> 
<property name="eclipelink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
<property name="eclipelink.jdbc.password" value="mypass"/>

But then I would recommend to solve "that other problem" and stay with 2.0

Here is an example of JPA 2.0 over EclipseLink that works ( notice persistence version="2.0" ):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="eclipselinktest" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <!-- list all classes -->
    <class>com.codesmuggler.model.User</class>

    <properties>
      <!-- some properties needed by persistence provider:
        - driver
        - db url
        - db user name
        - db user password -->
      <property name="javax.persistence.target-database" value="PostgreSQL"/>
      <property name="javax.persistence.logging.level" value="INFO"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb"/>
      <property name="javax.persistence.jdbc.user" value="testuser"/>
      <property name="javax.persistence.jdbc.password" value="testpassword"/>

      <!-- for testing purpose every time application is launched drop and create tables
        in production mode - this line should be removed or commented out
      -->
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />

    </properties>

  </persistence-unit>     
</persistence>

Take a look at the example in full

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号