Hi again,

If you have this requirement, the only way i see is using the datasource
itself with plain JDBC.

// get the datasource injected
@Resource(...) DataSource ds;
...

// get a connection from the datasource
Connection ....

...

Jean-Louis


Jean-Louis MONTEIRO wrote:
> 
> Hi Johannes ,
> 
> The specification says that you can create a query without any active
> transaction 
> http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String)
> http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String)
> .
> 
> But, if you look Query specification 
> http://java.sun.com/javaee/5/docs/api/javax/persistence/Query.html#executeUpdate()
> http://java.sun.com/javaee/5/docs/api/javax/persistence/Query.html#executeUpdate()
>  
> you will see that you need to have a active transaction that's why you get
> this TransactionRequiredException.
> 
> AFAIK, only searching is allowed without transaction.
> 
> Hope it helps.
> Jean-Louis
> 
> 
> 
> 
> Johannes Stamminger-2 wrote:
>> 
>> 
>> Hi,
>> 
>> 
>> I run into troubles with having a stateless bean trying to execute a 
>> tablespace/database creation sql statement (bwo createNativeQuery):
>> 
>> With beans default behaviour (container managed transaction) I run into a 
>> postgres exception
>> 
>> org.postgresql.util.PSQLException: ERROR: CREATE TABLESPACE cannot run
>> inside 
>> a transaction block
>> 
>> (roles and user creations before this work like a charme)
>> 
>> 
>> With "native" jdbc connection I experienced this being executable only
>> with 
>> enabling auto-commit and not using transactions. So I tried with
>> switching 
>> auto-commit on for the EntityManager created from an injected 
>> EntityManagerFactory.
>> 
>>     @PersistenceUnit(unitName = "valuesDb")
>>     private EntityManagerFactory fEntityManagerFactory;
>>     private EntityManager fEntityManager;
>> 
>>     @PostConstruct
>>     private void initEntityManager() {
>>         fEntityManager = fEntityManagerFactory.createEntityManager();
>>         fEntityManager.setFlushMode(FlushModeType.AUTO);
>>     }
>> 
>> Now the statement no longer get's executed at all as already the openjpa 
>> refuses with
>> 
>>     [junit] Caused by: <openjpa-1.2.0-r422266:683325 nonfatal user error> 
>> org.apache.openjpa.persistence.TransactionRequiredException: Can only
>> perform 
>> operation while a transaction is active.
>>     [junit]     at 
>> org.apache.openjpa.kernel.BrokerImpl.assertActiveTransaction(BrokerImpl.java:4380)
>>     [junit]     at 
>> org.apache.openjpa.kernel.QueryImpl.assertBulkModify(QueryImpl.java:1655)
>>     [junit]     at 
>> org.apache.openjpa.kernel.QueryImpl.update(QueryImpl.java:1038)
>>     [junit]     at 
>> org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:809)
>>     [junit]     at 
>> org.apache.openjpa.kernel.QueryImpl.updateAll(QueryImpl.java:884)
>>     [junit]     at 
>> org.apache.openjpa.kernel.QueryImpl.updateAll(QueryImpl.java:880)
>>     [junit]     at 
>> org.apache.openjpa.kernel.DelegatingQuery.updateAll(DelegatingQuery.java:565)
>>     [junit]     at 
>> org.apache.openjpa.persistence.QueryImpl.executeUpdate(QueryImpl.java:338)
>> 
>> 
>> Same happens with annotating the class with (without switching
>> auto-commit on)
>> 
>> @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
>> 
>> and/or
>> 
>> @TransactionManagement(TransactionManagementType.BEAN)
>> 
>> 
>> 
>> Is it not possible to execute this really special statement (one that 
>> executeUpdate refuses to within a transaction)? And/Or what am I doing
>> wrong?
>> 
>> 
>> (*Please* do not start a discussion on the necessity of
>> database/tablespace 
>> creation ;-), this would be of no help for me currently)
>> 
>> 
>> 
>> For completeness: my persistence.xml looks minimalistic:
>>     <persistence-unit name="valuesDb">
>>         <jta-data-source>valuesDbJtaDs</jta-data-source>
>>         <non-jta-data-source>valuesDbNonJtaDs</non-jta-data-source>
>>     </persistence-unit>
>> 
>> The connection properties are set by way of properties to the
>> InitialContext 
>> creation:
>> properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
>>                       
>> "org.apache.openejb.client.RemoteInitialContextFactory");
>>         
>> properties.setProperty("valuesDbJtaDs",
>> "new://Resource?type=DataSource");
>> properties.setProperty("valuesDbJtaDs.JdbcDriver",
>> Driver.class.getName());
>> properties.setProperty("valuesDbJtaDs.JdbcUrl", ...);
>> properties.setProperty("valuesDbJtaDs.UserName", ...);
>> properties.setProperty("valuesDbJtaDs.Password", ...);
>> properties.setProperty("valuesDbJtaDs.JtaManaged", "true");
>> 
>> properties.setProperty("valuesDbNonJtaDs",
>> "new://Resource?type=DataSource");
>> properties.setProperty("valuesDbNonJtaDs.JdbcDriver",
>> Driver.class.getName());
>> properties.setProperty("valuesDbNonJtaDs.JdbcUrl", ...);
>> properties.setProperty("valuesDbNonJtaDs.UserName", ...);
>> properties.setProperty("valuesDbNonJtaDs.Password", ...);
>> properties.setProperty("valuesDbNonJtaDs.JtaManaged", "false");
>> 
>> 
>> 
>> OpenEJB newby,
>> Johannes
>> 
>> This email (including any attachments) may contain confidential and/or
>> privileged information or information otherwise protected from
>> disclosure. If you are not the intended recipient, please notify the
>> sender immediately, do not copy this message or any attachments and do
>> not use it for any purpose or disclose its content to any person, but
>> delete this message and any attachments from your system. Astrium
>> disclaims any and all liability if this email transmission was virus
>> corrupted, altered or falsified.
>> ---------------------------------------------------------
>> Astrium GmbH Vorsitzender des Aufsichtsrates: Thomas Mueller -
>> Geschaeftsfuehrung: Evert Dudok (Vorsitzender), Dr. Reinhold Lutz,
>> Guenter Stamerjohanns, Josef Stukenborg
>> Sitz der Gesellschaft: Muenchen - Registergericht: Amtsgericht Muenchen,
>> HRB Nr. 107 647  
>> 
>> Weitere Informationen ueber EADS Astrium @ http://www.astrium.eads.net/
>> 
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/TransactionRequiredException-vs-must-not-execute-within-transaction-tp25066470p25069232.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Reply via email to