gpt4 book ai didi

java - Java SE 中的 UserTransaction 问题

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:50 24 4
gpt4 key购买 nike

为了在没有 Java EE 容器的情况下尝试 Java SE 中的声明式事务管理,我只是将 Glassfish 附带的事务 Servlet 示例转换为 Java SE。请参阅原始 Servlet 和随附的修改后的 Java SE 代码。

我正在使用:

  1. JDK 7
  2. weld-se-2.2.0.Alpha2.jar
  3. glassfish4\glassfish\modules\javax.transaction-api.jar
  4. glassfish4\glassfish\modules\javax.inject.jar
  5. glassfish4\glassfish\modules\javax.interceptor-api.jar
  6. glassfish4\glassfish\modules\weld-osgi-bundle.jar
  7. META-INF 文件夹中存在空白 beans.xml 文件。
  8. 我使用带有命令“org.jboss.weld.environment.se.StartMain”的批处理文件从命令行运行该程序。

问题:

  1. 当我使用 @Inject 注入(inject) UserTransaction 时,出现以下错误:

    线程“main”中的异常org.jboss.weld.exceptions.DeploymentException:WELD-001408:带有限定符 @Default 的 UserTransaction 类型的依赖关系不满足在注入(inject)点 [BackedAnnotatedField] @Inject TestSE.userTransaction在 TestSE.userTransaction(TestSE.java:0)

    因此,我用 @Resource 替换了 @Inject。

  2. 当使用@Resource时,程序运行正常,但没有显示正确的事务行为。即使 Bean 类型是强制的,Bean 也会被注入(inject)并正常工作,并且后续的 userTransaction.begin() 会抛出空指针异常。所有 6 个交易属性似乎都有一个奇怪的行为。

方式肯定有问题,我正在尝试执行它。有人可以帮我纠正这个问题吗?

非常感谢您的帮助。

BeanBase.java

public class BeanBase {
public String getId() {
return "ObjectId for this bean is " + this + "";
}
public String getId(String s) {
return "ObjectId for this bean is " + this + " and "+ s;
}
}

BeanMandatory.java

import javax.transaction.Transactional;

@Transactional(value = Transactional.TxType.MANDATORY)
public class BeanMandatory extends BeanBase {

}

BeanSupports.java

import javax.transaction.Transactional;

@Transactional(value = Transactional.TxType.SUPPORTS)
public class BeanSupports extends BeanBase {
}

BeanRequiresNew.java

import javax.transaction.Transactional;

@Transactional(value = Transactional.TxType.REQUIRES_NEW)
public class BeanRequiresNew extends BeanBase {
}

BeanRequired.java

import javax.transaction.Transactional;

@Transactional(value = Transactional.TxType.REQUIRED)
public class BeanRequired extends BeanBase {
}

BeanNotSupported.java

import javax.transaction.Transactional;

@Transactional(value = Transactional.TxType.NOT_SUPPORTED)
public class BeanNotSupported extends BeanBase {
}

BeanNever.java

import javax.transaction.Transactional;

@Transactional(value = Transactional.TxType.NEVER)
public class BeanNever extends BeanBase {
}

原始事务Servlet代码:

package transactional;

import javax.interceptor.InvocationContext;
import javax.inject.Inject;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import javax.transaction.*;
import java.io.IOException;


@WebServlet(name="TransactionalServlet", urlPatterns={"/TransactionalServlet"})
public class TransactionalServlet extends HttpServlet {

public static ServletOutputStream m_out;

@Inject
UserTransaction userTransaction;

@Injecthytggt
BeanMandatory beanMandatory;

@Inject
BeanNever beanNever;

@Inject
BeanNotSupported beanNotSupported;

@Inject
BeanRequired beanRequired;

@Inject
BeanRequiresNew beanRequiresNew;

@Inject
BeanSupports beanSupports;


/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String transactionalInterceptor = request.getParameter("TransactionalInterceptor");

m_out = response.getOutputStream();
m_out.println("<HTML>");
m_out.println("<HEAD>");
m_out.println("<title>CDI Sample Application for TransactionScoped Annotation</title>");
m_out.println("</HEAD>");
m_out.println("<BODY>");
m_out.println("TransactionalInterceptor value is -> " + transactionalInterceptor);
m_out.println("<BR><BR>");

if (transactionalInterceptor.equalsIgnoreCase("MANDATORY")) {
try {
m_out.println("<b>Scenario: Invoking outside transaction. Should get an error.</b>");
m_out.println("<BR><BR>");
m_out.println(beanMandatory.getId());
m_out.println("If you see this, it means there is something wrong!");
m_out.println("<BR><BR>");
} catch (Exception transactionalException) {
if (transactionalException.getCause() instanceof TransactionRequiredException) {
m_out.println("Got TransactionRequiredException for transactionalException.getCause() as expected.");
m_out.println("<BR><BR>");
} else {
m_out.println("If you see this, it means there is something wrong!");
m_out.println(transactionalException.getMessage());
m_out.println("<BR><BR>");
}
}
try {
userTransaction.begin();
m_out.println("<b>Scenario: Invoking within a transaction.</b>");
m_out.println("<BR><BR>");
m_out.println(beanMandatory.getId());
m_out.println("<BR><BR>");
userTransaction.commit();
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
} else if (transactionalInterceptor.equalsIgnoreCase("NEVER")) {
try {
m_out.println("<b>Scenario: Invoking outside transaction.</b>");
m_out.println("<BR><BR>");
m_out.println(beanNever.getId());
m_out.println("<BR><BR>");
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
try {
userTransaction.begin();
m_out.println("<b>Scenario: Invoking within a transaction. Should get an error.</b>");
m_out.println("<BR><BR>");
m_out.println(beanNever.getId());
m_out.println("If you see this, it means there is something wrong!");
m_out.println("<BR><BR>");
} catch (Exception transactionalException) {
if (transactionalException.getCause() instanceof InvalidTransactionException) {
m_out.println("Got InvalidTransactionException for transactionalException.getCause() as expected.");
m_out.println("<BR><BR>");
} else {
m_out.println("If you see this, it means there is something wrong!");
m_out.println(transactionalException.getMessage());
m_out.println("<BR><BR>");
}
} finally {
try {
userTransaction.rollback();
} catch (Exception e) {
m_out.println("Got unexpected exception in finally rollback for NEVER" + e.getMessage());
}
}
} else if (transactionalInterceptor.equalsIgnoreCase("NOT_SUPPORTED")) {
try {
m_out.println("<b>Scenario: Invoking outside transaction.</b>");
m_out.println("<BR><BR>");
m_out.println(beanNotSupported.getId());
m_out.println("<BR><BR>");
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
try {
userTransaction.begin();
m_out.println("<b>Scenario: Invoking within a transaction. Transaction is suspended during the method call. </b>");
m_out.println("<BR><BR>");
m_out.println(beanNotSupported.getId());
userTransaction.commit();
m_out.println("<BR><BR>");
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
} else if (transactionalInterceptor.equalsIgnoreCase("REQUIRED")) {
try {
m_out.println("<b>Scenario: Invoking outside transaction. Transaction would be started automatically for the method call.</b>");
m_out.println("<BR><BR>");
m_out.println(beanRequired.getId());
m_out.println("<BR><BR>");
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
try {
userTransaction.begin();
m_out.println("<b>Scenario: Invoking within a transaction.</b>");
m_out.println("<BR><BR>");
m_out.println(beanRequired.getId());
m_out.println("<BR><BR>");
userTransaction.commit();
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
} else if (transactionalInterceptor.equalsIgnoreCase("REQUIRES_NEW")) {
try {
m_out.println("<b>Scenario: Invoking outside transaction. Transaction would be started automatically for the method call.</b>");
m_out.println("<BR><BR>");
m_out.println(beanRequiresNew.getId());
m_out.println("<BR><BR>");
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
try {
userTransaction.begin();
m_out.println("<b>Scenario: Invoking within a transaction. NEW Transaction would be started automatically for the method call. </b>");
m_out.println("<BR><BR>");
m_out.println(beanRequiresNew.getId());
m_out.println("<BR><BR>");
userTransaction.commit();
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
} else if (transactionalInterceptor.equalsIgnoreCase("SUPPORTS")) {
try {
m_out.println("<b>Scenario: Invoking outside transaction. Method is executed outside transaction. </b>");
m_out.println("<BR><BR>");
m_out.println(beanSupports.getId());
m_out.println("<BR><BR>");
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
try {
userTransaction.begin();
m_out.println("<b>Scenario: Invoking within a transaction. Method is executed within transaction context.</b>");
m_out.println("<BR><BR>");
m_out.println(beanSupports.getId());
m_out.println("<BR><BR>");
userTransaction.commit();
} catch (Exception e){
m_out.println("If you see this, it means there is something wrong!");
m_out.println(e.getMessage());
m_out.println("<BR><BR>");
}
}

m_out.println("</BODY>");
m_out.println("</HTML>");
}

/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


}

我的 Java SE 示例 TestSE.java:

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.transaction.UserTransaction;
import javax.transaction.*;
import javax.enterprise.event.Observes;

import org.jboss.weld.environment.se.events.ContainerInitialized;

public class TestSE {
//@Inject //since this was throwing an exception, I used @Resource
@Resource
UserTransaction userTransaction;

@Inject
BeanMandatory beanMandatory;

@Inject
BeanNever beanNever;

@Inject
BeanNotSupported beanNotSupported;

@Inject
BeanRequired beanRequired;

@Inject
BeanRequiresNew beanRequiresNew;

@Inject
BeanSupports beanSupports;

public void process(@Observes ContainerInitialized init)
{
// TODO Auto-generated method stub
String command = null;
BufferedReader br = null;
boolean loop = true;

try
{
while(loop)
{
System.out.println("> 1 - Mandatory, 2 - Never, 3 - Not Supported, 4 - Required, 5 - Requires New, 6 - Supports, q - Quit");
System.out.println("Enter your choice and hit enter:");
br = new BufferedReader(new InputStreamReader(System.in));
command = br.readLine();
System.out.println("Received command[" + command + "]");

switch(command)
{
case "1":
System.out.println("Processing Mandatory");

try
{
System.out.println("Scenario: Invoking outside transaction. Should get an error.");
System.out.println(beanMandatory.getId());
System.out.println("If you see this, it means there is something wrong!");
}
catch (Exception transactionalException)
{
if (transactionalException.getCause() instanceof TransactionRequiredException)
{
System.out.println("Got TransactionRequiredException for transactionalException.getCause() as expected.");
}
else
{
System.out.println("2 If you see this, it means there is something wrong!");
System.out.println(transactionalException.getMessage());
}
}

try
{
userTransaction.begin();
System.out.println("Scenario: Invoking within a transaction.");
System.out.println(beanMandatory.getId());
userTransaction.commit();
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

break;
case "2":
System.out.println("Processing Never");

try
{
System.out.println("Scenario: Invoking outside transaction.");
System.out.println(beanNever.getId());
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

try
{
userTransaction.begin();
System.out.println("Scenario: Invoking within a transaction. Should get an error.");
System.out.println(beanNever.getId());
System.out.println("If you see this, it means there is something wrong!");
}
catch (Exception transactionalException)
{
if (transactionalException.getCause() instanceof InvalidTransactionException)
{
System.out.println("Got InvalidTransactionException for transactionalException.getCause() as expected.");
}
else
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(transactionalException.getMessage());
}
}
finally
{
try
{
userTransaction.rollback();
}
catch (Exception e)
{
System.out.println("Got unexpected exception in finally rollback for NEVER" + e.getMessage());
}
}

break;
case "3":
System.out.println("Processing NotSupported");

try
{
System.out.println("Scenario: Invoking outside transaction.");
System.out.println(beanNotSupported.getId());
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

try
{
userTransaction.begin();
System.out.println("Scenario: Invoking within a transaction. Transaction is suspended during the method call. ");
System.out.println(beanNotSupported.getId());
userTransaction.commit();
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

break;
case "4":
System.out.println("Processing Required");

try
{
System.out.println("Scenario: Invoking outside transaction. Transaction would be started automatically for the method call.");
System.out.println(beanRequired.getId());
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

try
{
userTransaction.begin();
System.out.println("Scenario: Invoking within a transaction.");
System.out.println(beanRequired.getId());
userTransaction.commit();
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

break;
case "5":
System.out.println("Processing RequiresNew");

try
{
System.out.println("Scenario: Invoking outside transaction. Transaction would be started automatically for the method call.");
System.out.println(beanRequiresNew.getId());
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

try
{
userTransaction.begin();
System.out.println("Scenario: Invoking within a transaction. NEW Transaction would be started automatically for the method call. ");
System.out.println(beanRequiresNew.getId());
userTransaction.commit();
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

break;
case "6":
System.out.println("Processing Supports");

try
{
System.out.println("Scenario: Invoking outside transaction. Method is executed outside transaction. ");
System.out.println(beanSupports.getId());
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}

try
{
userTransaction.begin();
System.out.println("Scenario: Invoking within a transaction. Method is executed within transaction context.");
System.out.println(beanSupports.getId());
userTransaction.commit();
}
catch (Exception e)
{
System.out.println("If you see this, it means there is something wrong!");
System.out.println(e.getMessage());
}
break;
case "q":
System.out.println("Quitting...");
loop = false;
}// eof switch
}// eof for
}
catch(Exception excp)
{
System.out.println("An exception has occured.");
excp.printStackTrace();
}
}// eof process

}//eof class

最佳答案

请查看此链接 https://github.com/luiz158/usertransaction-in-java-se

我阅读了您的问题并进行了此测试。

再见

关于java - Java SE 中的 UserTransaction 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21874885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com