You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.
View groupJoin the community to find out what other Atlassian users are discussing, debating and creating.
Using the Java API in ScriptRunner/groovy I am trying to retrieve all attachments for a page and then create a new one if it currently does not exists.
Everything is working so far, except the creation of the new attachment. Clearly, I'm missing something and community insight would be super helpful! I'm fairly new to Java/ScriptRunner/Groovy programming.
Code:
Attachment newFile = new Attachment("filename.csv", "text/csv", 6014, "new file");
Per: https://docs.atlassian.com/atlassian-confluence/6.0.3/com/atlassian/confluence/pages/Attachment.html, that should be all that is needed to construct a new Attachment object.
Once the attachment is created I would use AttachmentManager to save the file to the page with content from InputStream/ByteArray.
Does the file/content actually need to exist on the server somewhere before I can create the attachment and save to the page?
However, I receive the following errors in the log. <snippet>
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:873) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:710) at jdk.internal.reflect.GeneratedMethodAccessor80.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) at com.sun.proxy.$Proxy75.commit(Unknown Source)
Hello @Matt
Not sure what is going on there, but I usually use `AttachmentService` from our java-api over the `AttachmentManager`
Some questions to help me understand what's going on with that specific API usage :
@Hasnae, thank you very much for you reply!
A bit of background. This is Confluence Server, 7.3.1 and this is being written through ScriptRunner so I am not sure what JDK but I'd assume it's in the 8.x family.
As stated in this article, the error thrown before the "org.springframework.transaction.UnexpectedRollbackException" exception may be the culprit.
Looking at the logs, I see this before the "UnexpectedRollbackException":
2020-05-19 21:09:53,309 INFO [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview username:] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment: GroupAdmin_audit_2020.csv v.1 (0) null
2020-05-19 21:09:53,314 ERROR [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview ] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment content
-- referer: /plugins/servlet/scriptrunner/admin/jobs/edit/07a91bc6-58be-4aea-b31a-0acc9f2e5c8d?REF=EDACA9A69B5BE5EA19B534CAA0CB76C490997B3BD7A0F23D3E9100000004 | url: /rest/scriptrunner/latest/scheduled-jobs/com.onresolve.scriptrunner.canned.confluence.jobs.CustomScheduledJob/preview | traceId: 4b20be416167a207 | userName: | sr.execution.id: [814937b7-1d59-49a8-9f66-06963a8e334d]
2020-05-19 21:09:53,317 WARN [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview username:] [confluence.impl.hibernate.ConfluenceHibernateTransactionManager] doRollback Performing rollback. Transactions:
->[PluginReadWriteTx]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #1888224564)
-- referer: /plugins/servlet/scriptrunner/admin/jobs/edit/07a91bc6-58be-4aea-b31a-0acc9f2e5c8d?REF=EDACA9A69B5BE5EA19B534CAA0CB76C490997B3BD7A0F23D3E9100000004 | url: /rest/scriptrunner/latest/scheduled-jobs/com.onresolve.scriptrunner.canned.confluence.jobs.CustomScheduledJob/preview | traceId: 4b20be416167a207 | userName: | sr.execution.id: [814937b7-1d59-49a8-9f66-06963a8e334d]
I've bolded the part which I believe is causing the issue. It appears the attachment is null. So now I'm back to the drawing board of creating a new file attachment for a page without first writing the file to the filesystem.
Can the AttachmentService accomplish this?
Many Thanks!
-Matt
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
AHA !
I was hoping for this attachment being null error to surface again, so I can reproduce it consistently.
I have vague memories of "something somewhere" requiring a new hibernate transaction, which in this case means some objects are null. I will make some time this week/next to help come up with a workaround
Thank you so much for the clarification of how the code is executed, extremely useful to my team !
:thinking: I don't think the `AttachmentService` allows for that at the moment, looks like a good improvement to add to it.
Cheers
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Any help formulating a work around would be wonderful! I tried to do this again with saving the file to the Confluence temp dir first and then uploading but I'm still seeing "org.springframework.transaction.UnexpectedRollbackException" error but it does not show the attachment being null.
2020-05-20 21:11:05,963 ERROR [http-nio-8090-exec-472 url:/rest/scriptrun...heduledJob/preview ] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment content
-- referer: https://confluence-test-aws..com/plugins/servlet/scriptrunner/admin/jobs/edit/07a91bc6-58be-4aea-b31a-0acc9f2e5c8d?REF=1DE612E880BB9A7862649FC39A75860773AE5264966C670D5B3600000001 | url: /rest/scriptrunner/latest/scheduled-jobs/com.onresolve.scriptrunner.canned.confluence.jobs.CustomScheduledJob/preview | traceId: 97633fdaa713cb21 | userName: | sr.execution.id: [75ae8dc5-02c3-4272-9d0b-d052f95377d1]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Alright, I am starting my Friday just now, busy morning, but making time for it this afternoon.
Thanks for your patience !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello everyone, I am a product manager in the Jira Cloud team focused on making sure our customers have a delightful experience using our products. Towards that goal, one of the areas which is extr...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.