简体   繁体   中英

Configuring JTA in Tomcat7

I am using JEE5 with Postgresql, When I try to persist the object the output says

Could not find datasource: java:jdbc/postgres javax.naming.NameNotFoundException: the name jdbc/postgres is not asociated with this context

In context.xml (Tomcat7) I have

<Context>  
  <Resource name="jdbc/postgres" auth="Container"
            type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
            url="jdbc:postgresql://127.0.0.1:5433/peopledb"
            username="postgres" password="password" maxActive="20" maxIdle="10"
            maxWait="-1"/>
</Context>

In persistence.xml I have:

<persistence-unit name="People">    

    <jta-data-source>java:jdbc/postgres</jta-data-source>
        <properties>
           <property name="hibernate.hbm2ddl.auto" value="update" />
           <property name="hibernate.show_sql" value="true" />
        </properties>
</persistence-unit>

In my persistence bean I have

@Stateless
public class PeopleDAOImpl implements PeopleDAO {
  EntityManagerFactory emf = Persistence.createEntityManagerFactory("People");

  @Override
  public void persistPeople(People pep) {
    EntityManager em = emf.createEntityManager();

    em.persist(pep);
  }
}

Tomcat names the context resources differently than Glassfish. You defined the name in context.xml as jdbc/postgres , but in the persistence.xml you have to use java:comp/env/jdbc/postgres .

A late reply, but maybe still useful for others:

JTA can be used in Tomcat with componentized implementations like Atomikos ( https://www.atomikos.com ).

Cheers

PS: if you want help in setting things up you may want to consider a free trial of Atomikos - which includes Tomcat support / setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM