• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception - when create a new factory by Persistence class

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I need to create factory in run time because i dont know the shema names.
A new schema is allocated to the user after he register to the system.

I am working in Jboss 4.2.

I have a stateless session bean with the following method:

public void createEntityManagerFactoryWithPersistenceAPI() {
try {
Hotel h = new Hotel("Bla","Bla");
//****************** Map configuration
Map configOverrides = new HashMap();
configOverrides.put("javax.persistence.jtaDataSource", "java:/TestDS");
configOverrides.put("hibernate.hbm2ddl.auto", "create");
configOverrides.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
configOverrides.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

configOverrides.put("hibernate.default_schema", "Test");

EntityManagerFactory programmaticEmf =
Persistence.createEntityManagerFactory("Demo",configOverrides);
new InitialContext().rebind("java:/TestEMF", programmaticEmf);
}catch(Exception e){

}
}


I get an exception when i try to create the factory:
19:14:49,492 ERROR [STDERR] javax.persistence.PersistenceException: org.hibernate.HibernateException: The chosen transaction strategy requir
es access to the JTA TransactionManager
19:14:49,492 ERROR [STDERR] at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:720)
19:14:49,492 ERROR [STDERR] at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
19:14:49,492 ERROR [STDERR] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
19:14:49,492 ERROR [STDERR] at service.impl.HotelServiceBean.createEntityManagerFactoryWithPersistenceAPI(HotelServiceBean.java:83)

if i set to the following to the properties:
configOverrides.put("javax.persistence.transactionType", "RESOURCE_LOCAL");

the factory is created, but when i try to use it i get an exception:
18:36:54,138 ERROR [JDBCTransaction] JDBC commit failed

java.sql.SQLException: You cannot commit during a managed transaction!

at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.jdbcCommit(BaseWrapperManagedConnection.java:543)


My DS:
<datasources>
<local-tx-datasource>
<jndi-name>TestDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>1234</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->


My persistence.xml file:
<persistence>
<persistence-unit name="Demo">
<jta-data-source>java:/InsiteDS</jta-data-source>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>


Thank you!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic