Hibernate active transaction
In my service class I would like to have something like:
class ClientService {
// Authorize
// Returns true: Authorization successful
// Returns false: Authorization failed
public boolean authorize(String id, String password) {
//Here I would like to check if an active transaction exists.
//If it exists, use that one, else create a new session and start
//a new transaction.
//For example:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
if(!session.SOMEMETHOD_CHECK_IF_TRANSACTION_IS_ACTIVE) {
session.beginTransaction();
}
Client client = clientDAO.get(id);
if (client != null) {
if (client.getPassword().equals(password)) {
logger.debug("Authorization successful. ID: " + client.getId() + ", password: " + client.getPassword());
return true;
} else {
logger.error("Authorization unsuccessful");
return false;
} else {
logger.debug("Authorization unsuccessful. No client exists with ID: " + id);
return false;
}
}
}
Notice the commented text after the method head. I'm not so familiar with Hibernate, but I think it would be great if the service methods check if transaction exists, use it, otherwise create a new one and close it.
If it is possible, is there any performance reasons (or others) that I should have in mind? Or is it any other way to perform this kind of thing?
Best regards
hibernate
add a comment |
In my service class I would like to have something like:
class ClientService {
// Authorize
// Returns true: Authorization successful
// Returns false: Authorization failed
public boolean authorize(String id, String password) {
//Here I would like to check if an active transaction exists.
//If it exists, use that one, else create a new session and start
//a new transaction.
//For example:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
if(!session.SOMEMETHOD_CHECK_IF_TRANSACTION_IS_ACTIVE) {
session.beginTransaction();
}
Client client = clientDAO.get(id);
if (client != null) {
if (client.getPassword().equals(password)) {
logger.debug("Authorization successful. ID: " + client.getId() + ", password: " + client.getPassword());
return true;
} else {
logger.error("Authorization unsuccessful");
return false;
} else {
logger.debug("Authorization unsuccessful. No client exists with ID: " + id);
return false;
}
}
}
Notice the commented text after the method head. I'm not so familiar with Hibernate, but I think it would be great if the service methods check if transaction exists, use it, otherwise create a new one and close it.
If it is possible, is there any performance reasons (or others) that I should have in mind? Or is it any other way to perform this kind of thing?
Best regards
hibernate
add a comment |
In my service class I would like to have something like:
class ClientService {
// Authorize
// Returns true: Authorization successful
// Returns false: Authorization failed
public boolean authorize(String id, String password) {
//Here I would like to check if an active transaction exists.
//If it exists, use that one, else create a new session and start
//a new transaction.
//For example:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
if(!session.SOMEMETHOD_CHECK_IF_TRANSACTION_IS_ACTIVE) {
session.beginTransaction();
}
Client client = clientDAO.get(id);
if (client != null) {
if (client.getPassword().equals(password)) {
logger.debug("Authorization successful. ID: " + client.getId() + ", password: " + client.getPassword());
return true;
} else {
logger.error("Authorization unsuccessful");
return false;
} else {
logger.debug("Authorization unsuccessful. No client exists with ID: " + id);
return false;
}
}
}
Notice the commented text after the method head. I'm not so familiar with Hibernate, but I think it would be great if the service methods check if transaction exists, use it, otherwise create a new one and close it.
If it is possible, is there any performance reasons (or others) that I should have in mind? Or is it any other way to perform this kind of thing?
Best regards
hibernate
In my service class I would like to have something like:
class ClientService {
// Authorize
// Returns true: Authorization successful
// Returns false: Authorization failed
public boolean authorize(String id, String password) {
//Here I would like to check if an active transaction exists.
//If it exists, use that one, else create a new session and start
//a new transaction.
//For example:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
if(!session.SOMEMETHOD_CHECK_IF_TRANSACTION_IS_ACTIVE) {
session.beginTransaction();
}
Client client = clientDAO.get(id);
if (client != null) {
if (client.getPassword().equals(password)) {
logger.debug("Authorization successful. ID: " + client.getId() + ", password: " + client.getPassword());
return true;
} else {
logger.error("Authorization unsuccessful");
return false;
} else {
logger.debug("Authorization unsuccessful. No client exists with ID: " + id);
return false;
}
}
}
Notice the commented text after the method head. I'm not so familiar with Hibernate, but I think it would be great if the service methods check if transaction exists, use it, otherwise create a new one and close it.
If it is possible, is there any performance reasons (or others) that I should have in mind? Or is it any other way to perform this kind of thing?
Best regards
hibernate
hibernate
asked Jan 31 '11 at 18:48
kungcckungcc
66741842
66741842
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
lweller's answer is a more appropriate approach than my answer, but you can check the state of a transaction by calling session.getTransaction().isActive()
See the javadoc for Hibernate Transaction.
I think, session.getTransaction() surely returns always a active transaction... So not working for me(atlest).... is there any other solution?
– Manan Shah
Jul 17 '13 at 6:30
add a comment |
Basically you can call session.beginTransaction(); in any case as it is specified by JavaDoc of Hibernate:
Begin a unit of work and return the associated Transaction object. If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction.
But I would seriously consider the usage of a framework for transation management, like spring
I totally agree. At our place of work, they don't use Spring and it's causing all sorts of hibernate transaction issues.
– blackpanther
Apr 10 '14 at 15:13
add a comment |
((SessionImpl)session).isTransactionInProgress() will give the transaction is active or not without creating any new transaction
New contributor
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
This seems to be more like a comment but not an answer.
– Pavan
Jan 18 at 15:56
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Hardik Mishra
Jan 18 at 19:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4854746%2fhibernate-active-transaction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
lweller's answer is a more appropriate approach than my answer, but you can check the state of a transaction by calling session.getTransaction().isActive()
See the javadoc for Hibernate Transaction.
I think, session.getTransaction() surely returns always a active transaction... So not working for me(atlest).... is there any other solution?
– Manan Shah
Jul 17 '13 at 6:30
add a comment |
lweller's answer is a more appropriate approach than my answer, but you can check the state of a transaction by calling session.getTransaction().isActive()
See the javadoc for Hibernate Transaction.
I think, session.getTransaction() surely returns always a active transaction... So not working for me(atlest).... is there any other solution?
– Manan Shah
Jul 17 '13 at 6:30
add a comment |
lweller's answer is a more appropriate approach than my answer, but you can check the state of a transaction by calling session.getTransaction().isActive()
See the javadoc for Hibernate Transaction.
lweller's answer is a more appropriate approach than my answer, but you can check the state of a transaction by calling session.getTransaction().isActive()
See the javadoc for Hibernate Transaction.
edited Jan 24 '13 at 4:11
Akhil Jain
10.1k124580
10.1k124580
answered Feb 1 '11 at 7:54
jpkrohlingjpkrohling
12.1k12937
12.1k12937
I think, session.getTransaction() surely returns always a active transaction... So not working for me(atlest).... is there any other solution?
– Manan Shah
Jul 17 '13 at 6:30
add a comment |
I think, session.getTransaction() surely returns always a active transaction... So not working for me(atlest).... is there any other solution?
– Manan Shah
Jul 17 '13 at 6:30
I think, session.getTransaction() surely returns always a active transaction... So not working for me(atlest).... is there any other solution?
– Manan Shah
Jul 17 '13 at 6:30
I think, session.getTransaction() surely returns always a active transaction... So not working for me(atlest).... is there any other solution?
– Manan Shah
Jul 17 '13 at 6:30
add a comment |
Basically you can call session.beginTransaction(); in any case as it is specified by JavaDoc of Hibernate:
Begin a unit of work and return the associated Transaction object. If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction.
But I would seriously consider the usage of a framework for transation management, like spring
I totally agree. At our place of work, they don't use Spring and it's causing all sorts of hibernate transaction issues.
– blackpanther
Apr 10 '14 at 15:13
add a comment |
Basically you can call session.beginTransaction(); in any case as it is specified by JavaDoc of Hibernate:
Begin a unit of work and return the associated Transaction object. If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction.
But I would seriously consider the usage of a framework for transation management, like spring
I totally agree. At our place of work, they don't use Spring and it's causing all sorts of hibernate transaction issues.
– blackpanther
Apr 10 '14 at 15:13
add a comment |
Basically you can call session.beginTransaction(); in any case as it is specified by JavaDoc of Hibernate:
Begin a unit of work and return the associated Transaction object. If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction.
But I would seriously consider the usage of a framework for transation management, like spring
Basically you can call session.beginTransaction(); in any case as it is specified by JavaDoc of Hibernate:
Begin a unit of work and return the associated Transaction object. If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction.
But I would seriously consider the usage of a framework for transation management, like spring
answered Jan 31 '11 at 19:10
lwellerlweller
8,99932937
8,99932937
I totally agree. At our place of work, they don't use Spring and it's causing all sorts of hibernate transaction issues.
– blackpanther
Apr 10 '14 at 15:13
add a comment |
I totally agree. At our place of work, they don't use Spring and it's causing all sorts of hibernate transaction issues.
– blackpanther
Apr 10 '14 at 15:13
I totally agree. At our place of work, they don't use Spring and it's causing all sorts of hibernate transaction issues.
– blackpanther
Apr 10 '14 at 15:13
I totally agree. At our place of work, they don't use Spring and it's causing all sorts of hibernate transaction issues.
– blackpanther
Apr 10 '14 at 15:13
add a comment |
((SessionImpl)session).isTransactionInProgress() will give the transaction is active or not without creating any new transaction
New contributor
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
This seems to be more like a comment but not an answer.
– Pavan
Jan 18 at 15:56
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Hardik Mishra
Jan 18 at 19:31
add a comment |
((SessionImpl)session).isTransactionInProgress() will give the transaction is active or not without creating any new transaction
New contributor
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
This seems to be more like a comment but not an answer.
– Pavan
Jan 18 at 15:56
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Hardik Mishra
Jan 18 at 19:31
add a comment |
((SessionImpl)session).isTransactionInProgress() will give the transaction is active or not without creating any new transaction
New contributor
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
((SessionImpl)session).isTransactionInProgress() will give the transaction is active or not without creating any new transaction
New contributor
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Jan 18 at 15:55
vkmvkm
1
1
New contributor
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
vkm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
This seems to be more like a comment but not an answer.
– Pavan
Jan 18 at 15:56
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Hardik Mishra
Jan 18 at 19:31
add a comment |
2
This seems to be more like a comment but not an answer.
– Pavan
Jan 18 at 15:56
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Hardik Mishra
Jan 18 at 19:31
2
2
This seems to be more like a comment but not an answer.
– Pavan
Jan 18 at 15:56
This seems to be more like a comment but not an answer.
– Pavan
Jan 18 at 15:56
1
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Hardik Mishra
Jan 18 at 19:31
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Hardik Mishra
Jan 18 at 19:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4854746%2fhibernate-active-transaction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown